How to Validate Field Submissions in Drupal

| Drupal
How to Validate Field Submissions in Drupal

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.

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:

Add a field in Drupal 8

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".

Add a field validation rule set in Drupal 8

  • Choose the field that you have created.
  • Choose your validation rules.

Add a field validation rule in Drupal 8

  • 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:

Testing your validated field in Drupal 8


About the author

Daniel is a web designer from UK, who's a friendly and helpful part of the support team here at OSTraining.