0

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 ;

}
  • post your code, what have you tried so far ? – berramou Jan 18 at 11:31
  • what is the name of your module ? – berramou Jan 18 at 14:18
  • form_registration_event I've renamed it here for privacy reasons. I have debugged in PHP Storm and I can see the function being called. – BearWithNoHair Jan 18 at 14:35
  • I've updated code with latest code version. – BearWithNoHair Jan 18 at 14:40
0

You need to check values of checkbox field in $form_state while setting it "checked" in line

$form['field_show_end_date'][LANGUAGE_NONE]['#attributes']['checked'] = 'checked';

This would be something like

$form['field_show_end_date'][LANGUAGE_NONE]['#attributes']['checked'] = isset($form_state['values']['field_show_end_date'][LANGUAGE_NONE][0]['value']) ? $form_state['values']['field_show_end_date'][LANGUAGE_NONE][0]['value'] : 'checked';

I haven't tested this code, but it should work just fine or it will give you idea about how to fix your problem. Hope it helps...cheers!

  • Thanks @RajeevK However, it looks like $form_state is not available in my form_alter_function. – BearWithNoHair Jan 21 at 11:06
  • @BearWithNoHair It won't be available initially, but when validation fails and again alter is called, there should be data in it. – RajeevK Jan 22 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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