"Add another item" using #ajax in hook_field_widget_form I'm trying to create a custom widget for my module which will contain a textfield to hold a question(textfield), Question style (select box), potential answers (typed into textarea).

I'd then like an "add another item" button so the client can add as many questions as they like.

I've got a basic ajax widget working but i'm not sure if I've constructed it correctly because when I want to validate the data in hook_field_validate I don't seem to be able to access each question seperately.

I thought i'd be able to validate each question before the next is added by ajax but this doesn't seem to be working.

Asssuming this code (below) is correct, can you please advise how I can validate each time the "add another item" is clicked.

My hook_field_widget_form looks like this:

    function keep_warm_field_widget_form(&amp;$form, &amp;$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  $element['question_wrapper'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('Questions'),
    // Set up the wrapper so that AJAX will be able to replace the fieldset.
    '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
  }
  for ($i = 0; $i &lt; $form_state['num_names']; $i++) {


    //other form elements
    $element['question_wrapper'][$i]['question'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Question number '. $form_state['num_names']),

    );

  }

  $element['question_wrapper']['add_question'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add another question'),
    '#submit' =&gt; array('ajax_example_add_more_add_one'),
    // See the examples in ajax_example.module for more details on the
    // properties of #ajax.
    '#ajax' =&gt; array(
      'callback' =&gt; 'ajax_example_add_more_callback',
      'wrapper' =&gt; 'names-fieldset-wrapper',
    ),
  );
  if ($form_state['num_names'] &gt; 1) {
    $element['question_wrapper']['remove_name'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Remove one'),
      '#submit' =&gt; array('ajax_example_add_more_remove_one'),
      '#ajax' =&gt; array(
        'callback' =&gt; 'ajax_example_add_more_callback',
        'wrapper' =&gt; 'names-fieldset-wrapper',
      ),
    );
  }

return $element;
}


And my ajax callback

function ajax_example_add_more_callback($form, $form_state) {
  $form_state['num_names']++;
  return $form['field_question'];
}
Looks like you're just missing a #tree property:


  Used to allow collections of form elements. Normally applied to the "parent" element, as the #tree property cascades to sub-elements. Use where you previously used ][ in form_ calls. For more information, see #tree and #parents in the handbook.


e.g.

$element['question_wrapper'] = array(
  '#type' =&gt; 'fieldset',
  '#title' =&gt; t('Questions'),
  // Set up the wrapper so that AJAX will be able to replace the fieldset.
  '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
  '#tree' =&gt; TRUE,
);


And then in the validate handler:

foreach ($form_state['values']['question_wrapper'] as $key =&gt; $value) {
  if (is_numeric($key) &amp;&amp; something_is_wrong_with($value['question'])) {
    form_set_error("question_wrapper][$key][question", 'Error');
  }
}