Requiring at least one field

In fact, the form might have multiple fields with the same validator rules but at least one of them is required. We can't use neither the notEmpty validator nor HTML 5 required attribute for all of them.

Fortunately, this can be done easily by two things.

First, use the selector option to define validators for all fields. So, we don't need to indicate the validator rules for each separate field.

Tip

Using the class name prefixed by js-, js-email-address for example, is a good practice. It lets other developer in your team know that the field will be interactived by JavaScript. So it shouldn't be removed when other designers refactor the markup or CSS classes.

Next, use the callback validator to check if one of fields is not empty. And then update the result of callback validator via the updateFieldStatus() method.

In the sample code above, we use the getElements() method to get all the email elements.

Related validators

The following validators might be useful to you.