3

I am facing a issue while adding multiple custom validation to a form. I can only add a single custom validation to my form. How to add multiple validations.

Eg:

this.user = this.fb.group({
  name: ['', Validators.required],
  account: this.fb.group({
    email: ['', Validators.required],
    confirm: ['', Validators.required]
  }, { validator: emailMatcher })
});

I want to add more validations like: { validator: [emailMatcher, matchPassword, othervalidation]}

Is there something in angular 2 which can help me in this.

1
  • or email: ["", [Validators.required, this.validateService.emailValidator]] Commented Aug 25, 2017 at 11:27

1 Answer 1

3

Instead of :

{ validator: emailMatcher })

Use syntax:

{ validators:  [emailMatcher, matchPassword, othervalidation]}

Good luck.

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

4 Comments

I think it should be 'validators' instead of 'Validators' because of angular.io/api/forms/FormBuilder#group
you're probably @EmonZan , not sure maybe the syntax changed from Angular 2 to current Angular 8, would need to bring out old Angular 2 project and test
@EmonZan If Gary doesn't edit the answer and you still think you are correct, please add your answer as a separate answer instead of changing significantly the Gary Holland answer.
I've modified to validators, as that is what the docs say. However, the original syntax Validators does still appear to work (as of Angular 9).

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.