Out of the box, Drupal offers only a single type of validation for fields; required or not required. For most use cases this is fine, however, it can be a little difficult to define your own custom validation logic. What if you need to validate a field value and make sure it’s unique?
The Field validation module allows you to define custom validation rules using Drupal’s administration interface. The module ships with a lot of its own validators: “Plain text”, “Specific value(s)” and much more.
If none of the validators meet your requirement, you can write your own by implementing a validator plugin. Because validators are Ctools plugin, they are easy to maintain as each one gets its own file.
In this tutorial, we’ll use Field validation to validate a field value and make sure it’s unique.
If you use Drush, run the following command:
drush dl field_validation ctools drush en field_validation_ui
Unique Field Value
The module comes with one useful validator which checks if a field value is unique. Of course, you could you implement this type of validation with custom code, but Field validation can handle it with just a few easy clicks.
I’ve created a field called “ID number” which I’ve attached to the Article content type. Let’s now configure a validator which will be used to make sure the field value is unique.
1. Click on Structure from the toolbar, click on “Field Validation” and “Add”.
2. In the Name field enter in “id_number_validation”, “ID number validation” in the “Rule name” field.
3. From “Entity type” select Node, from “Bundle” select Article and from “Field name” select “ID number” (the field we want to validate.)
Because we added the field to the Article content type, we selected Node from the “Entity type” and Article from the Bundle. If this field was on the user entity then you would select User as the “Entity type” and Bundle.
4. Finally, select value from the Column drop-down.
At this point, we’re given the validation a name and selected which field we want to validate. Let’s now configure the validator.
5. Click on the Validator drop-down and select “Unique values”.
You can further configure the validator from the field-set, below the drop-down. For now leave it as is.
6. The final thing we need to do is create a validation message. Enter in “[field-name] must be unique.” into the “Custom error message” field. You may have noticed that I used “[field-name]” in the message. You can use certain tokens in your messages. Click on “Replacement patterns” to see which ones are available.
Now if you try and create two articles with the same “ID number” value you should see “ID number must be unique.”.
You may be thinking to yourself, this is all good but can I export the configuration into code? Yes you can. Field validation has complete Features integration.
Just select your custom validation from the “Field validation” component and add it to your feature.
I’ve only shown you one small bit of what Field validation can really do. But trust me, spend some time playing with it and you’ll be amazed how powerful it is.
I have to mention that there’s a sub-module called “Field validation extras”. This sub-module ships more specific validators for “Postal code”, “Color(HTML5)” and much more. Make sure you review this sub-module when searching for all available validators.