Validating form data
We will expand on the form created in the last recipe to add validation. We will add validation information to form elements in the company form. There is also a validateForm
method that can be used to programmatically identify errors and prevent form submission.
Getting ready
This recipe will use the form class created in the Creating a custom form and saving configuration changes recipe.
How to do it…
- First, we will make the form elements
required
. This will prevent submitting the form without providing values. Update the form elements inbuildForm
to match the following:$form['company_name'] = [
'#type' => 'textfield',
'#title' => 'Company name',
'#required' => TRUE,
'#default_value' => $company_settings...