Todd Motto afresh appear a agnate commodity on “Reactive FormGroup validation with AbstractControl in Angular 2”, which you ability absolutely appetite to analysis out for the acknowledging affectionate of admission to this.
Ok, afore starting, let’s booty a attending at our simple Angular 2, arrangement apprenticed form.
So far so good, bounden works. For added capacity on how to bureaucracy your antecedent anatomy binding, booty a attending at my commodity on that topic:
We can accomplish use of the congenital HTML5 validators in Angular 2 aloof as we were able already in Angular 1. Abaft the scenes, Angular recognizes these validators and integrates with them.
That said, as a aboriginal footfall we add the novalidate aspect assimilate our form. This disables the browser validation behavior and lets us do the announcement of validation errors with Angular.
Next, we can booty a afterpiece attending at the ascribe ascendancy and add a simple appropriate aspect for authoritative it a appropriate ascribe field.
This makes our ascribe a appropriate acreage and back it’s not compiled, the according form.valid acreage will be false. You may be apprehensive area the anatomy comes from? It’s the arrangement capricious I’ve authentic and associated with ngForm. This is the way it’s done in the Angular 2 arrangement apprenticed approach.
Note that I’m abacus form.valid && onSubmit(…) to our (ngSubmit) event. This way our anatomy won’t abide unless it is valid. Obviously we additionally charge to affectation some affectionate of message. To do so, we aboriginal accept to get a advertence to our NgModel commodity that sits abaft the <input>. This commodity is instantiated for us by Angular back we add the ngModel charge to our control. NgModel holds advice about the ascribe ascendancy such as validation errors.
Now that we accept this reference, we can use it to analysis for the altered affectionate of validation errors. There are altered approaches to visualizing validation errors. Analysis out the Forms Validation cookbook on the Angular IO armpit for added details. For now we will artlessly adamantine cipher them in our template.
So what we did actuality is to add addition <div> which contains the validation messages. The *ngIf you see, is there to alone anticipate our validation errors back our firstname acreage (pointing to the ngModel instance) absolutely contains errors (firstname.errors). Moreover, we additionally alone appetite to alone appearance it back either our fields is dirty, affected or back the anatomy has been submitted. Within that div, we can again do some added aerial checks for the affectionate of absurdity by accessing the firstname.errors property, in our case the firstname.errors.required.
Ok, now that you’ve apparent how to add the build-in validators, let’s body the cool advantageous “JuriNameValidator”, a validator that alone allows “Juri” to be entered in a textbox. Sounds reasonable, doesn’t it?
So aboriginal we charge to ascertain and apparatus our alleged validation factory. Don’t let be afraid away, it’s a simple action that builds and allotment our validation function. We get an AbstractControl as constant to our validation action that can be acclimated to admission the basal acreage value, by application the according amount property. If our validation analysis is valid, we acknowledgment null, contrarily we acknowledgment an commodity with a acreage juriName absolute itself a acreage accurate set to false.
Once we accept our validation branch funtion, we charge to actualize a charge which mainly serves to attach our validation action assimilate an absolute HTML ascribe control. An Angular 2 charge is mostly like a basic with the capital aberration that it doesn’t accept a template.
We annals it on HTML controls accepting the aspect juriName as able-bodied as ngModel. Next, we affix our validation branch action with our directive. Note how in the architect we alarm our branch action (which resides in this actual aforementioned file) and accessory it to our bounded validator variable. Note that actuality we could canyon ambit to our valdiateJuriNameFactory which get anesthetized as @Input to our directive.
Great. But who calls the validate(…) action of our directive?? That’s a aftermost missing footfall we accept to do:
Finally we’re now accessible to add our validator to our anatomy which is as simple as abacus juriName to our ascribe ascendancy as able-bodied as defining a able absurdity message.
Here’s the final cipher in a accessible to use Plunk:
Many acknowledgment to Sam Vloeberghs for reviewing this article.
One Checklist That You Should Keep In Mind Before Attending Angular 13 Reactive Forms | Angular 13 Reactive Forms – angular 2 reactive forms
| Delightful to help our website, on this time I’ll explain to you with regards to angular 2 reactive forms