0

I want to submit a value if a checkbox is unchecked (on a node edit form).

Using theme_form_element I can add the hidden field as follows:

$variables['element']['#children'] = '$variables['element']['#name'] . '" value="0" />' . $variables['element']['#children'];

But on submit, because value 0 is not in the allowed values list, I get the 'An illegal choice has been detected. Please contact the site administrator.' error message.

How can I bypass this validation and/or submit the value 0 for each unchecked checkbox?

1 Answer 1

0

Take a look at the answer to Custom validation for a form? This tells you how to add your own validation handler to the form. You will need to implement the form_alter like they show to add your custom validation, but also remove the validation that is throwing the error when the value is 0. (You can do this by unsetting that index in the array.) Then, for your custom validation method, my recommendation is to have a validation handler something like:

function my_custom_validation(&$form, &$form_state) {
  if ($form_state['values']['my_checkbox_field'] !== 0) {
    // Call the validation handler you removed in your form_alter
    // I might set the name of the method to a variable in form_state
    the_validation_handler_you_removed($form, $form_state);
  }
}

where you call the original validation handler from your method if the checkbox value is not the specific value you're looking for, in your case 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.