As OSTraining member asked us how to validate fields in Drupal 8.
In this particular example, they wanted to make sure that every entry in a text field was unique.
For this tutorial, you will need to download, install and enable the following modules.
- Field Validation
- Chaos Tools suit
- Entity API
- Inline Form Errors (this is an experimental module in the Drupal Core)
Once those modules are enabled, here's how to set up the validation:
- Go to Structure > Content types > Manage fields
- Add a field. In the image below, we're using a Text (formatted) field:
Once you have saved the field, you can now add validation.
- Go to Structure > Field validation.
- Click "Add field validation rules".
- Select the entity type and then content type with your field.
- Click "Create new field validation rule set".
- Choose the field that you have created.
- Choose your validation rules.
- Add and update the field validation rules set.
- I would advise you to also run cron on your site.
- Now you can test the validation rule: