The Problem
Angular
does support the concept of conditional required through the built-in directive ng-required
. And it works great in simple scenarios. Consider the following example where address2
is only required when address1
is not empty.
This can be done using ng-required
as shown above. But what if we also want address2
to be ‘not required’ when address1
is empty? There’s no such thing as ng-not-required
. If we take a look at the source code of ng-required
, we can see that it is considered to be as valid only when the ngModel
is NOT empty:
There’s nothing wrong with ng-required
being implemented this way, as its name implies, it is used for adding required
restriction/validation on a form element conditionally. But it does become limited or useless when we have more complex problems to solve. For example:
- Conditional not required is not supported as we have already talked about above.
- Support for requireness of one or more elements in a group is very limited.
The Solution
Create not-required
directive
We can fix problem #1 with a custom directive that is slightly modified from ng-required
, we call demo-not-required
:
As you can see, as opposite to ng-required
, demo-not-required
considers a form element to be valid when its ngModel IS empty rather than not.
Requireness of one or more elements in a group
With ng-required
and demo-not-required
in hand as building blocks, we can solve more complex problems.
At Least One Required
This is one of the common requirements in form validations. Among multiple form elements, at least one is required. In other words, it is ok to have more than one elements being non-empty, but it is not ok to all the elements being empty.
For example, we may want ‘phone number’ and ‘email’ to be ‘at least one required’. And this can easily be done using ng-required
:
At Most One Required
This is kind of the opposite to ‘at least one required’. This is to say that among multiple form elements, at most one is required. In other words, it is fairly ok to have all the elements left blank, but if we were to put values, no more than one element can have values.
For example, some restaurant offers free salad or soup when you order any meal. And you can choose not to have either of them. This relationship can be implemented using demo-not-required
we created above:
Only One Required
This relationship is kind of a combination of ‘at most one required’ and ‘at least one required’ and can indeed be built as such. Take online payment as an example. Let’s say you must leave either your credit card or debit card number as a method of payment:
Above implementation looks ok but rather redundant. Since the logic in this case is a combination of the two, we can create a new directive ‘demo-one-required’ that combines the logic of the two existing directives:
through which, the solution becomes clearer:
Refactoring
At this point, the directives we have built, namely: demo-not-required
, demo-one-required
all look similar. In fact, they only differ in the validation logic. What we can do is to build a general purpose conditional requireness validation directive that support all group and non-group relationship, including all the scenarios we have talked about. And the actual ‘rule’ that determines the group/non-group relationship is configurable.
For example, let’s say this directive we are going to build is called demo-required
:
The controller will look like this:
The configurable rules:
Then finally in the html, we simply specify the groupId for demo-required
:
The code
The directives we have built can be found in plunker.
The final demo-required
is still a work in progress, and can also be found in plunker.