Validating form data

The Form API requires all form classes to implement the \Drupal\Core\Form\FormInterface. The interface defines a validation method. The validateForm method is invoked once a form has been submitted and provides a way to validate the data and halt the processing of the data if required. The form state object provides methods for marking specific fields as having the error, providing a user experience tool to alert your users specifically to the problem input.

This recipe will be based on the custom module and form created in the Creating a form section of this chapter. We will be validating the length of the submitted field.

Getting ready

This recipe will be using the module and custom form created in the first Creating a form ...

Get Drupal 8: Enterprise Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.