1

I have form with check boxes. The user checks the boxes and hits the submit button. If validation fails an error message is displayed. I also want all of the user's checks to be cleared so they can try again. Here's my code, but it does not clear the checks? Thanks.

/**
 * Defines a form.
 */
function my_form($form, &$form_state) {

    $form['alerts'] = array(
        '#type' => 'checkboxes',
        '#options' => $options,
    );

    // the rest of my form

    if (isset($form_state['clear_checkboxes'])) {
        $form_state['values']['alerts'] = array_fill(0, count($form_state['values']['alerts']), 0);
    }
    return $form;

}

/**
 * Form validator.
 */
function my_form_validate($form, &$form_state) {

    // if validation fails set error message

    $form_state['rebuild'] = TRUE;
    $form_state['clear_checkboxes'] = TRUE;

}

1 Answer 1

1

You can unset your field in the form .Check for the array which has field array.

unset($form_state['field_checkbox']['und'][0]['value']); 

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.