How do I create custom validation for a form in Drupal?

Step 1: In order to create a custom form validation, you should use the “Field Validation” Drupal module, which you could download via the following link:

https://www.drupal.org/project/field_validation

Step 2: The installation of the module is pretty simple – you could do this by navigating to your Drupal administrative panel > Modules > Install new module and either paste the provided URL or download the archive file and upload it via the displayed form in “Install new module” section.
Step 3: After the above has been done, you could add a custom field validation easily.
Step 4: Usually it will display a custom error message when you attempt to add an article title, which is already used in your Drupal website. This can be done by navigating to your administrative panel > Structure > Property validation > Add.
Step 5: Next, you should enter the fields “Name”, “Rule name”, “Entity type”, “Bundle name”, “Property name” and “Validator”.
Step 6: Finally you have to enter a custom error message into the appropriate field and click the “Save” button.
Step 7: Now, you may proceed to adding a new article with name “Hosting title” via administrative panel > Content > Add content. Now when you attempt to add another article with the same name, the following message appears “ message: You already have article with the same title.”
Step 8: That’s it! Your custom validation form has been created successfully.

Related Articles