Skip to content

Instantly share code, notes, and snippets.

@anxp
Last active August 23, 2022 13:35
Show Gist options
  • Save anxp/1efe9f4c95044aababe61b43d3007813 to your computer and use it in GitHub Desktop.
Save anxp/1efe9f4c95044aababe61b43d3007813 to your computer and use it in GitHub Desktop.
How to correctly use #limit_validation_errors element of Form API in Drupal 7
<?php
//Sometimes we need to submit only part of the form. Sure, if form has #required fields, all they will be highlighted with red,
//and form will not be submitted if these elements have no value.
//To workaround this, we can use '#limit_validation_errors' option AND custom submit function for every 'Partial Submit' button.
//Interesting note: when we use '#limit_validation_errors', we will see ALL form fields in _validate function,
//but in _submit function will be visible ONLY elements, included in '#limit_validation_errors' array!!!
//Case 1. The most simple.
//We want to make a 'reset' button, or 'previous step' button, so it must work even if some REQUIRED form elements are empty:
function form_with_reset_button($form, &$form_state) {
$form['title'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['btn_reset'] = [
'#type' => 'submit',
'#value' => 'Reset',
'#name' => 'btn_reset',
'#limit_validation_errors' => [], //We don't want validate any errors for this submit button, because this is RESET button!
'#submit' => [
'btn_reset_handler',
],
];
return $form;
}
//Implementation of btn_reset_handler() can be something like:
function btn_reset_handler($form, &$form_state) {
$pressed_btn_name = $form_state['triggering_element']['#name'];
if ($pressed_btn_name != 'btn_reset') {return;}
/*
Here we can delete any additional elements of the form, if any.
unset ($form_state['user_added'], $form_state['storage'], or any custom added element of form_state...);
*/
$form_state['rebuild'] = FALSE; //And, obviously, we don't want rebuild the form.
}
//Case 2.
//We want to limit validation just to one form element. This means, ONLY this specified element will be checked on validation.
//If other required elements will be empty, they will be ignored and form will be passed to submit function.
function form_with_limited_validation_to_ONE() {
$form['first_text_input'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['second_text_input'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['btn_submit_first'] = [
'#type' => 'submit',
'#value' => 'Partial submit',
'#name' => 'btn_part_submit',
'#limit_validation_errors' => [['first_text_input']], //Only 'first_text_input' form element will be validated to be not empty.
//'second_text_input' will be ignored even despite it is #required => TRUE.
'#submit' => [
'btn_part_submit_handler',
],
];
return $form;
}
//Case 3.
//We want to limit validation to several elements. In this case, we need to specify each of them as array with one element,
//and all these arrays need to be elements of one big array.
function form_with_limited_validation_to_SEVERAL() {
$form['first_text_input'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['second_text_input'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['third_text_input'] = [
'#type' => 'textfield',
'#title' => 'Input title here:',
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
];
$form['btn_submit_first_second'] = [
'#type' => 'submit',
'#value' => 'Partial submit',
'#name' => 'btn_part_submit',
'#limit_validation_errors' => [['first_text_input',], ['second_text_input',]], //Only 'first_text_input' and 'second_text_input' form elements will be validated to be not empty.
'#submit' => [
'btn_part_submit_handler',
],
];
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment