Form

Drupal 7 form cancel button

ยง
by
Jesper bisgaard

When creating a form it is often required to make a cancel or back button, but this provides a number og issues with form validation but fortunetly there is a way to prevent these issues.

Create your cancel or back button.

$form['actions']['back'] = array( 
  '#type' => 'submit', 
  '#value' => t('Back'), 
); 

You can use either type: submit, image_button or button. In order to prevent validation of the form elements you need to add #limit_validation_errors and #submit to the element.

$form['actions']['back'] = array(
  '#type' => 'submit', 
  '#value' => t('Back'), 
  '#limit_validation_errors' => array(), 
  '#submit' => array('example_form_submit'), 
); 

#limit_validation_errors contains an array of elements to validate on submit and since it is empty in this case nothing is validated

In order for #limit_validation_errors to work you also need to specify a submit function to call otherwise the element attribute is ignored.

Subscribe to Form