I've got a two step process set up on a node edit form. The first step field (A) is a selection of a group, the second step field (B) is a select list that is filtered by what you chose in step one. This appears to work fine on its own. It saves fine, and the edit page loads with the proper values selected and filtered correctly.

However, when you add another unrelated text field elsewhere in the form (C), with unlimited values, the 'add another item' button on C will trigger a validation warning on the previously mentioned (B) field, whether it has values or not. It also only triggers when you try to add a THIRD item, not the second.

My understanding is that the 'add another item' button is triggering validation when clicked. It appears that my desired solution is to turn off validation when (C) is clicked using #limit_validation_errors.

I have two questions though:

#1 Why is there a validation error in the first place. Even when items are correctly populating the lists (demonstrated to work), it triggers an error. Shouldn't that be validating fine?

#2 How do I apply #limit_validation_errors? I'm not sure how to target it within $form / $form_state. The button ID changes when you add more items to it, so I'm concerned about that.

Any help would be greatly appreciated, as I worry this 'add another item' issue will cause problems down the line, where I expect to add more fields with that option.

Comments

lnicks’s picture

This error isn't limited to the 'add another button', it's being caused on all ajax buttons, example file fields with unlimited values.

Jaypan’s picture

Unfortunately, #limit_validation_errors isn't the solution you would hope it to be. Any values that are not validated are not passed onwards. So if you remove the validation from the element, you also cannot get the value from that field. As such, I've found this attribute to be mostly useless.

lnicks’s picture

Thanks for pointing that out Jaypan (and thank you for your continual answers / support within the community, I've been reading your AJAX article all day).

Edit: I was way off, still having problems

lnicks’s picture

Having problems still. I need to set default_sort_value. The $form_state array doesn't appear to have the values I want (viewing it from a kpr), and triggering element is what I believe is causing the validation issues. If a separate ajax event triggers it, triggering element will be incorrect?

Edit: My current understanding of the issue is that I need a passable value from $form_state, not an array, but I'm not sure how to access that. $default_sort_order appears to be getting an array.

function mymodule_form_alter(&$form, &$form_state, $form_id)
{       

    if ($form_id == 'committee_agenda_node_form') {
    // provide validation items
	
	$default_sort_value = isset($form_state['values']['og_group_ref']) ? $form_state['values']['og_group_ref'] : '';
		

    // ajax action element  
        $form['og_group_ref'][LANGUAGE_NONE][0]['default']['#ajax']        = array(
            // specify which element you're effecting,
            'wrapper' => 'input_related_events_wrapper',
            // specify callback function
            'callback' => 'input_related_events_callback',
            // optional set javascript event
            'event' => 'change'
   );	
				
	$form['field_related_meeting'][LANGUAGE_NONE]['#options'] = get_filtered_agenda_view($default_sort_value);

        // ajax result element - puts wrapper around effected element, so drupal knows what to alter
        $form['#prefix'] = '<div id="input_related_events_wrapper">';
        $form['#suffix'] = '</div>';                
    }
}