I have a form_form_id_alter function that sets some default values and a checkbox checked.
If I submit the form, with the checkbox unset and it fails validation, the re-rendered form re-sets the checkbox to a checked state (as per the form_alter), not the submitted state.
Is this expected behaviour? I would expect the submitted values to remain persistant.
Do I need to re-assign the submitted values in the form_validate function or am I doing something wrong?
I'm using this function to alter form:
// Form ID alter
function events_form_registration_event_node_form_alter(&$form, &$form_state, $form_id){
global $user;
$form['#validate'][] = 'events_form_registration_event_node_form_validate';
// If node add.
if(arg(1) == 'add'){
// Check if the user is an event editor and perform the form_alter
if(events_does_user_have_role($user)){
// Show end date, checkbox value checked
$form['field_show_end_date'][LANGUAGE_NONE]['#attributes']['checked'] = 'checked';
// Event status, list value Open
$form['field_event_status'][LANGUAGE_NONE]['#default_value'] = 'Open';
}
}
}
// Validate form.
function events_form_registration_event_node_form_validate(&$form, &$form_state){
// Get the current end date value from submitted form.
$current_end_date_value = ($form_state['values']['field_show_end_date'][LANGUAGE_NONE][0]['value']);
// Set the show_end_date element to have the submitted value.
$form['field_show_end_date'][LANGUAGE_NONE]['#attributes']['checked'] = $current_end_date_value ;
}