0

I am creating a form which has checkbox, and options for checkbox are coming dynamically. In submit function I am rebuilding form if validation fails. all values are rendering but previously checked checkbox is not checking.

below is the code:

function modulename_form(){
      $header = array();
      $rows = array();
      $conditions = '';
      $orderby = array( 'pass_lname' => 'ASC'); 
      $unmapped_data = capcup_get_unmapped_passengers_data($conditions , $orderby);

      foreach ($unmapped_data as $key => $value) {
        $user = user_load($value->pass_uid);
        $insertion = !empty($value->pass_insertion)? $value->pass_insertion . ' ': '';
        if (empty($buttons)) {
          $row['name'] = '<a href="mailto:'  . $user->mail . '">' . check_plain($value->pass_lname . ' ' . $insertion . $value->pass_fname) . '</a>';
        }
        else {
          $row['name'] = '<a target="_blank" href="/passagier/'  . $value->passenger_uid . '">' . check_plain($value->passenger_lname . ' ' . $insertion . $value->passenger_fname) . '</a>';
        }
        $row['phone'] = $value->pass_tel;
        $row['practicecode'] = $value->pass_practcode;    
          $form['active'] =  array(
          '#type' => 'checkbox',
          '#return_value' => $value->pass_uid,
          '#attributes' => array(
              'name' => array('nodes[' . $value->pass_uid . ']'),
            )
          );
          $row['active'] =  drupal_render($form['active']);
        $rows[$value->pass_uid] = $row;
      }

      $form['nodes'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => t('No passenger available.'),
      );
return $form;
}

submit function:

function modulename_submit($form, $form_state) {
  if (in_array('boat', $account->roles)) {
      $form_state['rebuild'] = TRUE;
      form_set_error('boat_user_info][emailaddress', 'Dit e-mailadres is al in gebruik.');
      return FALSE;
    }
}

I am writing form function like above because to get structure like below image: enter image description here

When form will rebuilt all fields will remembered there previous value but checkbox is not remembered its value. I tried by adding default_value and all but not worked. Please suggest me how can I achieve that.

1
  • Can you show me where and how you add #default_value for this checkbox in the form function?
    – argiepiano
    Apr 12, 2017 at 12:57

1 Answer 1

0

All form validations should be done in form validate function and not in submit function. Read this article

Hope it helps. :)

Your Answer

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

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