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 Development Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.