validationdrupal-7drupal-webform

Drupal 7 - Webform - Validation


I am using Drupal 7 installation and am using a webform to manage a submission of some data.

The fields are as follows:

  1. Date field which captures a date input
  2. Two textfields which capture numeric values
  3. Checkbox which captures a customer acceptance

I want to be able to allow the form submission based on whether:

What would be the cleanest way of accomplishing this in Drupal 7?


Solution

  • I ended up accomplishing this using hook_form_alter() and a callback function wired to $form['submit']['#validate'][].

    function mymodule_form_alter(&$form, &$form_state, $form_id) {
      if ($form_id == 'myform') {
        $form['submit']['#validate'][] = 'validation_function';
      }
    }
    
    function validation_function($form, &$form_state) {
      // Validation logic here
      // If in validation failed set error message here
    }