0

I was trying to modify the ajax_example.module, so I can validate value entered in the textfields. Below you'll find the code. But it seems that I can't access the field first name for example in order to validate it's value. I am still pretty new in Drupal 7 module development so I am not quite sure that the approach I used below is the appropriate one. Thanks in advance for your help.

  $form['ask_first_name'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ask me my first name'),
    '#ajax' => array(
      'callback' => 'ajax_example_autotextfields_callback',
      'wrapper' => 'textfields',
      'effect' => 'fade',
    )
  );
  $form['ask_last_name'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ask me my last name'),
    '#ajax' => array(
      'callback' => 'ajax_example_autotextfields_callback',
      'wrapper' => 'textfields',
      'effect' => 'fade',
    ),
  );

  $form['textfields'] = array(
    '#title' => t("Generated text fields for first and last name"),
    '#prefix' => '<div id="textfields">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we put automatically generated textfields'),
  );

  // Since checkboxes return TRUE or FALSE, we have to check that
  // $form_state has been filled as well as what it contains.
  if (!empty($form_state['values']['ask_first_name']) && $form_state['values']    ['ask_first_name']) {
    $form['textfields']['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First Name'),
    );
  }
  if (!empty($form_state['values']['ask_last_name']) && $form_state['values']['ask_last_name']) {
    $form['textfields']['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Click Me'),
  );

  return $form;
}

/**
 * Selects the piece of the form we want to use as replacement text and returns
 * it as a form (renderable array).
 *
 * @return renderable array (the textfields element)
 */
function ajax_example_autotextfields_callback($form, $form_state) {
  return $form['textfields'];
}

/**
 * implementing hoo_validate for the form
 */
function ajax_example_autotextfields_validate($form, &$form_state) {
  //$name = $form_state['values']['textfields']['first_name'];
  if ($form_state['values']['textfields']['first_name'] == 'King Kong') {
    form_set_error('first_name', t('Your own first name not allowed'));
  }
}

/**
 * hook_submit
 */
function ajax_example_autotextfields_submit($form, &$form_state) {
  $name = $form_state['values']['textfields']['first_name'];
  drupal_set_message(t('Good Job, %name', array('%name' => $name)));
}
?>
  • Try adding '#tree' => TRUE to $form['textfields'] – Clive Dec 18 '12 at 14:42
  • Try cutting out some of the code to it easier to read for the community. :) – Letharion Dec 18 '12 at 15:05
  • @Clive thank you a lot. It's been two days I am not sleeping because of that!!!! – nik Dec 18 '12 at 15:20

Your Answer

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

Browse other questions tagged or ask your own question.