How can I conditionally require form inputs with AngularJS?

Suppose we’re building an address book application (contrived example) with AngularJS.

We have a form for contacts that has inputs for email and phone number, and we want to require one or the other, but not both: We only want the email input to be required if the phone input is empty or invalid, and vice versa.

Angular has a required directive, but it’s not clear from the documentation how to use it in this case. So how can we conditionally require a form field? Write a custom directive?

6 Answers
6

Leave a Comment