Validating custom control

02-May-2016 14:39

The issue was that sometimes it was required, and other times it wasn't, but I didn't want to require just one field or the other.I wanted the whole control to register as required.Angular 1 has the handy ng Messages modules to help manage error messages and validation in forms.This post I’ll show how to build a custom messages component in Angular to easily manage validation similar to ng1’s ng Messages.First of all, it’s important to realise that we shouldn’t just create custom form controls right away, if there’s a native element (like ) that perfectly takes care of the job.

Instead if I USE A NORMAL REQUIRED FILED VALIDATION CONTROL THEN WITH IT USE CLIENT SIDE VALIDATION IT DOES EXECUTE PROPERLY.

In our example we are going to build a small form with three inputs, user name, email and profile description.

We will start with looking at our import from '@angular/core'; import from '@angular/platform-browser'; import from '@angular/forms'; import from '@angular/router'; import from './app.component'; import from './control-messages.component'; import from './validation.service'; @Ng Module() export class App Module This creates a new form with our desired controls.

The control in question was a month/year date control, which is was a little complicated, logic-wise.

So to illustrate the basic idea, I put together a quick plunker of the idea with a custom addition control.

Instead if I USE A NORMAL REQUIRED FILED VALIDATION CONTROL THEN WITH IT USE CLIENT SIDE VALIDATION IT DOES EXECUTE PROPERLY.

In our example we are going to build a small form with three inputs, user name, email and profile description.

We will start with looking at our import from '@angular/core'; import from '@angular/platform-browser'; import from '@angular/forms'; import from '@angular/router'; import from './app.component'; import from './control-messages.component'; import from './validation.service'; @Ng Module() export class App Module This creates a new form with our desired controls.

The control in question was a month/year date control, which is was a little complicated, logic-wise.

So to illustrate the basic idea, I put together a quick plunker of the idea with a custom addition control.

The first parameter in the control we leave empty as this lets you initialize your form control with a value. Angular has some built in Validators such as syntax.