How can I do complex validation on the forms for adding content? An example of what I want to do, is ensure that either one field or another is filled out. It is fine if both are filled out, or if either one is filled out, but not if neither is filled out. I'm interested in the best way of doing this kind of more complex (beyond just a single required field) validation in general, but the specific case above can serve as the specific answerable question here. Links to appropriate documentation/resources also appreciated.
2 Answers
The field validation module allows advanced validation rules without having to write custom code.
It includes (emphasis mine):
The following validators are currently included (7.x-2.x):
- Regular expression
- Numeric values (optionally specify min and / or max value)
- length (optionally specify min and / or max length)
- number of words (optionally specify min and / or max words )
- Plain text (disallow tags)
- Must be empty (Anti-Spam: Hide with CSS)
- Words blacklist
- number of selections (optionally specify min and / or max selections )
- Unique
- Match against a field
- Match against a property
- Specific value(s)
- Require at least one of several fields
- Equal values on multiple fields
- Unique values on multiple fields
- PHP Code (powerfull but dangerous)
- URL (support internal path and external url validation)
- Pattern (Regular expression lite)
- Date range2
Following validators are included in sub module field validation extras:
- Color(HTML5)
- Date(ISO)
- EAN number
- Field collection unique
- Integer values
- IP Address
- Numeric(HTML5, with the option to specify min/max/step)
- Regular expression (Perl-Compatible)
- Phone
- Required field
- Require some of several fields
- Postal code(depend on Postal Code Validation, support Address Field)
-
-
Do you have any experience with this module and "Require at least one of several fields". Looking at it, I couldn't quickly figure out how to use it, and there isn't any documentation except a lullabot article on using regex validation.– UltraBobMar 4, 2016 at 2:46
-
Not recently. I will have to have a play around with it to refresh my memory.– roobyMar 4, 2016 at 4:04
-
If you are creating forms using webforms module you can easily set all these dependencies. Without even writing code.