3

I have this forming group like so:

   channelInfo: this.fb.group({
    youTubeSubs: [null, [Validators.required, CustomValidators.number]],
    instagramFollowers: [null],
    twitterFollowers: [null],
    snapchatFollowers: [null],
    facebookLikes: [null]
  }),

Only the youTubeSubs is required, all the other number is optional. But if there is value, I need to make sure the value to be number using the CustomValidators.number provide by this component: Angular2 Validator

How can I allow null and number validator at the same time? So if value is empty null, it will show error. But if there is value, the error will show if the value is not number?

1
  • I think the empty error is caused by Validators.required. See this Demo Commented Mar 22, 2017 at 1:36

1 Answer 1

2

Just remove the Validators.required when validating the other fields like so..

channelInfo: this.fb.group({
    youTubeSubs: [null, [Validators.required, CustomValidators.number]],
    instagramFollowers: [null, [CustomValidators.number]],
    twitterFollowers: [null, [CustomValidators.number]],
    snapchatFollowers: [null, [CustomValidators.number]],
    facebookLikes: [null, [CustomValidators.number]]
}),
Sign up to request clarification or add additional context in comments.

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.