Drupal 7 validating form on ajax
In the validation function, you need to check the value of the title field, which is held in $form_state[‘values’][’title’].
If the value is “the quick fox jumped over the lazy dog” then set an error. The first parameter is the name of the field that has caused the error. The field specified will be highlighted with a red border, so the user has a clear visual cue as to which field has not validated.
You need to ensure that the user has added valid data and if not, show them a meaningful error message.
Validation functions are normally implemented in the module where the form is defined.
You need to add your validation function to that array by adding the following: $form['#validate'] = ‘starting_drupal_dev_form_validate’.
$form[‘validate’] is an array and you can add to that with $form['#validate'].
For this tutorial, you will need to download and enable a module called devel so that you can use its Drupal Print Message (dpm) function.
The users of our forms need not wait for the form data to travel back to the web server, get processed, and then return from the server with error messages flagging erroneous or missing input.
I'd been using conditional fields to change both the display of some fields and their requirement status based on user input in another field, but that worked only to control the display and to set the appearance of being required (the red asterisk).
It didn't actually enforce validation, and I spent much of yesterday trying various suggested solutions.
If you have not signed up for it, you can do so here.
In our implementation of hook_form_alter, we will have a look at the validation functions already set for the article node form.