Field Validation
Provides customizable field validation rules for any entity type through an intuitive admin interface.
field_validation
Overview
The Field Validation module allows site administrators to specify custom validation rules for fields on any content entity type (nodes, users, taxonomy terms, etc.) without writing any code. Unlike core field validation which is limited to basic constraints, this module provides a comprehensive set of 20 built-in validation rules covering common use cases like email validation, regular expressions, unique values, numeric ranges, and more.
The module uses Drupal's plugin system, allowing developers to extend it with custom validation rules. Validation rules are organized into "rule sets" that are attached to specific entity type and bundle combinations. Each rule can be configured to apply to specific user roles and can include conditional logic based on other field values.
The module integrates seamlessly with Drupal's constraint validation system, meaning validation errors appear inline on forms just like core validation errors. It also supports token replacement in error messages and rule settings for dynamic validation messages.
Features
- 20 built-in validation rule types including regex, email, phone, URL, unique values, numeric/integer ranges, date ranges, word count, character length, and more
- Create validation rule sets per entity type and bundle (e.g., one rule set for Article nodes, another for Page nodes)
- Role-based rule application - configure rules to only apply to specific user roles
- Conditional rule execution - rules can be applied based on other field values using operators like equals, not equals, greater than, less than, empty, not empty
- Token support in error messages and some rule settings for dynamic validation
- Integration with Drupal's core constraint validation system for consistent error display
- Extensible plugin architecture - developers can create custom validation rules
- Support for both base fields and configurable fields
- Multi-value field support with item count validation
- Field column selection for complex field types (e.g., validate only the 'value' column of a text field)
Use Cases
Enforce Unique Product SKUs
E-commerce sites can use the Unique validation rule to ensure that product SKU fields contain unique values across all products. Configure the rule with scope set to 'bundle' to restrict uniqueness to products only, not other content types.
Validate Phone Numbers by Country
International sites can validate phone number fields using country-specific formats. The Phone validation rule supports 24 countries with their specific phone number patterns, ensuring users enter correctly formatted numbers.
Require Minimum Content Length
Editorial workflows can enforce content quality by requiring a minimum word count or character length for body fields. The Length and Words rules can ensure articles meet minimum content requirements before publishing.
Prevent Profanity in User-Generated Content
Community sites can use the Words Blocklist rule to prevent users from submitting content containing profanity or inappropriate terms. Create a comma-separated list of blocked words.
Honeypot Anti-Spam Protection
Add a hidden text field using CSS and apply the 'Must be empty' validation rule. Spam bots will fill in all fields while real users won't see or fill the hidden field, blocking automated spam submissions.
Validate Date Ranges for Events
Event sites can use the Date Range rule to ensure event dates fall within acceptable ranges. The cycle feature allows validation of recurring patterns, like ensuring events only occur on certain days of the week.
Confirm Email Address
Use the Equal Values rule to require users to enter their email address twice and ensure both entries match, reducing typos in email addresses.
Validate Custom ID Formats
Organizations with specific ID formats (e.g., 'ABC-123-456') can use the Pattern rule to enforce these formats. The simplified pattern syntax (a=alpha, 9=digit, #=alphanumeric) makes it easy to define custom patterns.
Role-Specific Validation
Apply different validation rules based on user roles. For example, administrators might be exempt from certain validation rules while regular users must comply. Configure rules to apply only to specific roles.
Conditional Field Validation
Apply validation only when certain conditions are met. For example, require a phone number field only when the 'Contact by phone' checkbox is selected, using the condition settings on the rule.
Tips
- Use Token module to create dynamic error messages that include the field name, entity type, or user information
- Combine multiple rules on the same field for comprehensive validation (e.g., Length + Plain Text + Blocklist)
- Use the Condition feature to create conditional validation that only applies when other fields have specific values
- Export rule sets to configuration files for deployment across environments using Drupal's config export/import
- Create custom validation rules by extending ConfigurableFieldValidationRuleBase and implementing the validate() method
- The Pattern rule is simpler than Regex for common format validations - use 'a' for letters, '9' for digits, '#' for alphanumeric
- For multi-value fields, most rules validate each value individually - use Item Count to validate the number of values
- Base fields (like node title) are also supported - they appear in the field dropdown alongside bundle fields
Technical Details
Admin Pages 4
/admin/structure/field_validation
Lists all configured field validation rule sets. Each rule set is associated with a specific entity type and bundle combination. From this page you can add new rule sets, edit existing ones, or delete them.
/admin/structure/field_validation/add
Create a new validation rule set by selecting the target entity type and bundle. Only one rule set can exist per entity type/bundle combination.
/admin/structure/field_validation/manage/{rule_set}
Manage validation rules within a rule set. View, reorder, edit, or delete existing rules. Add new rules from the available validation rule types.
/admin/structure/field_validation/manage/{rule_set}/add/{rule_type}
Configure a new validation rule. All rules share common settings (field selection, error message, role restrictions, conditions) plus rule-specific options.
Permissions 1
Hooks 2
hook_entity_bundle_field_info_alter
Used by Field Validation to attach the FieldValidationConstraint to bundle fields that have validation rules defined.
hook_entity_base_field_info_alter
Used by Field Validation to attach the FieldValidationConstraint to base fields that have validation rules defined.
Troubleshooting 5
Clear all caches after adding or modifying rules. The module uses Drupal's constraint system which caches field definitions. Run 'drush cr' or clear caches via the admin UI.
Each entity type/bundle combination can only have one rule set. Edit the existing rule set to add more rules instead of creating a new one.
Check the 'Scope' setting. 'Entity' scope checks uniqueness across all entities of that type, while 'Bundle' scope only checks within the same bundle. Also check 'Published' and 'Per user' settings which further limit the scope.
Ensure your regex pattern includes delimiters (e.g., '/pattern/'). The Regex rule expects Perl-compatible regular expressions. Test your pattern using an online PCRE tester.
This is a known limitation with unlimited cardinality fields. The module documentation recommends setting #limit_validation_errors on the 'Add more' button to an empty array via custom code.
Security Notes 4
- The 'administer field validation rule set' permission is marked as 'restrict access' and should only be granted to trusted administrators
- Regular expression patterns (Regex rule) execute on user input - ensure patterns are properly tested to avoid ReDoS vulnerabilities
- The Blocklist rule performs case-insensitive matching - ensure your blocklist covers common variations of prohibited terms
- The Must Be Empty honeypot technique helps prevent spam but should be combined with other anti-spam measures for comprehensive protection