Learning about Form & Field Validation in Drupal 7
Blog

Learning about Form & Field Validation in Drupal 7

This article is to share my experience with Drupal CMS. It will be helpful , particularly for novice/beginner level Drupal programmers to build their knowledge block on Drupal framework. Even though you might be aware of a few functionalities mentioned, this article will guide you on the basics of ‘what should be used’, ‘when to be used’ and ‘why to use’.

I have started blogging again after a brief break to share my knowledge of Drupal. Before getting into any of the technical aspects, I would suggest you to try and understand the ‘How & why?’. This will help you concrete you knowledge about any form of technology. So let’s begin with a functionality instance of Drupal called “validation”. 

Terminology will be used in most of the forms where the end user has  the required data with self and is ready to validate the same before submission. It gives assurance that the data posted to table does not have any miscellaneous data. Proper validation of form data is important to protect your form from hackers and spammers.

Types of validations that we can perform on form:

Validate Drupal form using validate function

hook_form_alter() : By implementing hook_form_alter to the specific bundle and calling $form['#validate’][], it will list all of the validation functions for a given form. Here we can add our custom validation to the validation handler array using $form['#validate'][].

When you hit the node/add/* , you will find node_form_validate(), which is the core validator for node forms.

form validation tutorial 0

The above validate_handler would be useful when it comes to an existing node type, where the developer has full access to insert validation for a form using custom module.

Validate only Drupal form field element using custom function:

We don't need to add an entire form validation every time. We can add a validation handler for an individual form element using #element_validate. We can also construct our own element validators. 

To validate a specific field, we can use #element_validate, with the element specific validation function which will pass $form_state and $element.

Use a pre-defined validation handler to validate numbers/integers.

form validation tutorial 1

Instead of handling the entire form, #element_validate helps to trigger a specific field  validation and displaying custom error message.

Validate Only Individual form element with pre define validation handler:

Drupal has a lot of pre define validators. The only thing is that either we are not aware of it or we haven't used it before.
When you want to validate a number with a predefined handler, you don’t have to add the validation function as it already exists.

Drupal has already defined a handler that validates if the data is a number, called element_validate_number. So we need to add the following to our form element:

Just one line of code can handle validation. Here is a list of  pre-defined validation handlers.

  • date_validate - Validates the date type to prevent invalid dates (e.g., February 30, 2006).
  • element_validate_integer -Form element validation handler for integer elements.
  • element_validate_integer_positive - Form element validation handler
  • for integer elements that must be positive
  • element_validate_number - Form element validation handler for number elements.
  • password_confirm_validate - Validates a password_confirm element.


     form validation tutorial 2

     

An individual form element handler with pre define core function, does not allow you to write custom error messages. As of now there are a list of functions available to execute basic validation over form.

Validate function using _form_validate from drupal API

We can add custom handler to validate the data entered into the field.
NOTE: Name of the function. It is simply the name of the form followed by '_validate'. This is always the name of the default validation function.

form validation tutorial 3

Default drupal core provides hook to handle field validation. And the pattern is form_id_validate() where we can set our own message. 


Conclusion: Above methods are small pieces of validation towards entity and bundle field. Based on best practice and specific project requirement validation, handler should be used. 


GIT Source Code: https://github.com/xaiwant/D7validation/tree/master/article_rule