angular-validation

How to allow nested components to be tracked by their parent and get values from their parent in Angular?

China☆狼群 提交于 2019-12-04 12:15:21
I have a series of forms (each managed by 1 component). There is a pattern of inputs in these forms (e.g. many of them require to allow input of an address) that I have to refactor into re-usable components as they are used in multiple forms and I don't want to duplicate neither their logic nor their templates. Each re-usable component would have to have its logic have its template containing input tags and no <form> tag have its client validation constraints possibly receive initial values from its parent be able to return the value of its fields to the parent as an object (e.g. address:

Angular form updateValueAndValidity of all children controls

青春壹個敷衍的年華 提交于 2019-12-03 16:25:16
问题 In my Angular 4 app, I have a form with several controls. At some points I need to force the update of their validity, so I'm doing: this.form.get('control1').updateValueAndValidity(); this.form.get('control2').updateValueAndValidity(); this.form.get('control3').updateValueAndValidity(); // and so on.... and then: this.form.updateValueAndValidity(); this works fine. However I was wondering if there is a better way to accomplish the same thing, by just calling one method on the parent form.

Custom Validator Control Quantity in Reactive Forms

 ̄綄美尐妖づ 提交于 2019-12-02 06:46:24
I had a hard time implementing a custom validation in my reactive forms in Angular. I need to control the quantity. The quantity should not be more than the available quantity. The problem how can i get the total of all the quantity if each row has subrows. How will i able to compute the total of subrows and compare it to its parent row where the available quantity is found. Here's my code below. Here's also the link to my code PLEASE CLICK THIS LINK customValidator(campo1: string) { return (group: FormGroup): { [key: string]: any } => { const receive = group.controls[campo1]; //Change this

Angular - Dynamically add/remove validators

て烟熏妆下的殇ゞ 提交于 2019-11-30 07:09:34
问题 I have a FormGroup defined like below: this.businessFormGroup: this.fb.group({ 'businessType': ['', Validators.required], 'description': ['', Validators.compose([Validators.required, Validators.maxLength(200)])], 'income': [''] }) Now when businessType is Other , I want to remove Validators.required validator from description . And if businessType is not Other , I want to add back the Validators.required . I am using the below code to dynamically add/remove the Validators.required . However,

Angular2 Forms :Validations, ngControl, ngModel etc

时间秒杀一切 提交于 2019-11-27 11:43:55
Working on angular2 beta Forms . after alot of searching found nothing useful. hope here somebody help me. Basically i am bit confused how to use forms in angular2 in a proper manner (i.e using ngControl, ngFormControl etc). i have created one plnkr here http://plnkr.co/edit/fictP28Vqn74YqrwM1jW?p=preview here is my .html code:- <form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup"> <div class="col-md-7"> Name: <input type="text" id="name" placeholder="Name" class="form-control" ngControl="name"> </div> <div class="col-md-7"> Password: <input type="text" id=

Angular form validation: compare two fields

二次信任 提交于 2019-11-27 03:36:21
问题 In an Angular 4 application, how can I validate two fields of a form doing a comparison? For example, let's suppose that my form has a startDate and an endDate date fields and I want to make sure that the endDate must be bigger than the startDate . 回答1: When you want to implement validations containing one or more sibling (form)controls, you have to define the validator function on a level up/above that of the sibling controls. For ex: ngOnInit() { this.form = this.formbuilder.group({

Angular2 - FormControl Validation on blur

允我心安 提交于 2019-11-26 19:55:25
I'm looking at adding some basic email validation to check that the user has put in a correct email address. Currently using the method below, the validation updates as the user types, which looks odd when it errors after entering one character. validEmail(c: Control){ if(!c.value.match('[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?')){ return { validEmail: true }; } return null; } ctrlEmailAddress: Control = new Control('', Validators.compose([ Validators.required, this.validEmail])); I was wondering

Angular2 - FormControl Validation on blur

為{幸葍}努か 提交于 2019-11-26 07:28:20
问题 I\'m looking at adding some basic email validation to check that the user has put in a correct email address. Currently using the method below, the validation updates as the user types, which looks odd when it errors after entering one character. validEmail(c: Control){ if(!c.value.match(\'[a-z0-9!#$%&\\\'*+/=?^_`{|}~-]+(?:\\\\.[a-z0-9!#$%&\\\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\')){ return { validEmail: true }; } return null; }