83

I have two inputs:

  • First one on which I apply my custom validator
  • Second one which value I use in my custom validator (it is dynamic and editable)

If I apply my custom validator on the first input, then I focus the second one and change the value - I want to force first inputs re-validation...

At the moment it only re-validates first input when I change the value... Any suggestions?

At the moment when I focus the first input I can access it's reference:

<input
    name="mEnd"
    class="form-control"
    [(ngModel)]="endDate"
       ...
    #endDateInput="ngModel"
    (focus)="clog(endDateInput)"
>

I wonder can I trigger re-validation using the input formControl reference methods?

2
  • 3
    Have you write any code for that ? then please share here Commented Jun 26, 2017 at 11:49
  • similar post: stackoverflow.com/q/32260082/6908282 Commented Oct 21, 2024 at 9:51

4 Answers 4

136

You can update the validity of a formControl

form.controls['myControl'].updateValueAndValidity();

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to trigger it for all form controls in the form without having to loop them??
@Gagan the method is also available on a formgroup
But calling it on the formGroup doesn't check each control, only the parent.
But watch out calling it in a change event, the FormControl hasn't updated it's value yet. So your Validator might not behave as expected. Ex: one dropdown changes, validate the other inputs using this new value
26

I found this to be much better

this.myForm.markAllAsTouched();

9 Comments

Please explain, why would this be better? Marking an untouched field as touched does achieve validation, but at higher cost - AND it incorrectly marks fields as touched.
@HenryLowCZ because you toggle the touched flag whose purpose is to indicate that the user has focused the field, which might not be the case when you trigger it programatically. There could be other logic that evaulates the touched flag and gets misleaded.
Getting this error: Property 'markAllAsTouched' does not exist on type 'FormGroup not sure what type is supposed to be this.myForm
This is exactly what I need. It helps to show which fields are required right after the form was loaded.
The reason you don't wanna mark as touched spuriously is because you usually don't want to have your form input yell at the user until after the user has at least tried filling it out: e.g. you often don't not want a required field to turn red until after the user touches it or clicks the submit button. There are other ways to indicate that a field is required.
|
19

If you have template-driven form you can access form throw ViewChild decorator:

@ViewChild('myForm') public form: NgForm;

then, validate one field or the whole form group using method mentioned above:

this.form.controls.myControl.updateValueAndValidity();

Comments

0

Angular 12 and the other answers didnt work for me..so i added a hidden button on the form

 <button type="submit" [style.visibility]="'hidden'" #submitBtn>Submit</button>

On the controller ViewChild this button.

  @ViewChild('submitBtn') private buttonSubmit:ElementRef;

And force a click on the event i want.

  submitFormByKey(){
    this.buttonSubmit.nativeElement.click();
  }

This will trigger a submit and validate all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.