I'm writing a "Add one more" function with form ajax api. Now eveything works fine, except '#limit_validation_errors' breaking ajax call.
This is the code I'm writing:
function agl_jira_new_test_form($form, &$form_state) {
$form['textfield'] = array(
'#title' => 'Attachment',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['select'] = array(
'#title' => 'select',
'#type' => 'select',
'#options' => drupal_map_assoc(array('man', 'woman')),
'#required' => TRUE,
'#default_value' => isset($form_state['values']['select']) ? $form_state['values']['select'] : NULL,
'#ajax' => array(
'callback' => 'agl_jira_select_callback',
'wrapper' => 'names-fieldset-wrapper',
),
);
$form['test']['names_fieldset'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="names-fieldset-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (isset($form_state['values']['select']) && $form_state['values']['select'] == 'man') {
$form['test']['names_fieldset']['text'] = array(
'#title' => t('name'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['test']['names_fieldset']['attachment'] = array(
'#title' => t('File'),
'#type' => 'fieldset',
'#prefix' => '<div id="agl-jira-file-attach-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!isset($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['test']['names_fieldset']['attachment'][$i] = array(
'#type' => 'file',
'#title' => t('Name'),
);
}
$form['test']['names_fieldset']['add_name'] = array(
'#type' => 'submit',
'#value' => t('Add one more'),
//'#limit_validation_errors' => array(''),
'#submit' => array('agl_jira_example_add_more_add_one'),
'#ajax' => array(
'callback' => 'agl_jira_example_add_more_callback',
'wrapper' => 'agl-jira-file-attach-wrapper',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'submit',
);
return $form;
}
function agl_jira_select_callback($form, &$form_state) {
$form_state['rebuild'] = TRUE;
return $form['test']['names_fieldset'];
}
function agl_jira_example_add_more_callback($form, &$form_state) {
//$form_state['num_names']++;
return $form['test']['names_fieldset']['attachment'];
}
function agl_jira_example_add_more_add_one($form, &$form_state) {
$form_state['num_names']++;
$form_state['rebuild'] = TRUE;
}
function agl_jira_new_test_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
This code above works fine, and it looks like this:

So when user clicks "Add one more" button, a new 'Name' will show there. The thing is user MUST fill fields 'Attachment' and "name" textfields, as the two text fields are required. The solution to this problem is to use '#limit_validation_errors' => array(''), for $form['test']['names_fieldset']['add_name'] .
Then the problem comes. This "Add one more" function is totally broken. I click button "Add one more", this page just gets stuck there. No more "Name" file element will show. Is there any way to solve this problem? Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| good image.jpg | 28.59 KB | smiletrl |
Comments
Comment #1
smiletrl commentedComment #2
smiletrl commentedAnyway, I just found this might be done by adding the main logic to
function agl_jira_example_add_more_callbackto return the replacement. But this issue remains somehow with'#limit_validation_errors' => array(),Comment #3
smiletrl commentedAll right, I just found I need to add the first ajax level fields to '#limit_validation_errors'.