I'm implementing the ajax "add more" functionality to a Drupal form. Basically, the user can enter a variable number of records into the form. So, there's an 'add more' button which when click is supposed to show an additional row in the form. I've got this working. The problem is that when I set a field in the form to be required, And the user has left it blank, the new row doesn't appear. Rather validation errors appear. I want the validation, of course, but I don't want it firing on the ajax callback. I've searched throughout and found nothing. How do I disable this validation that runs on an AJAX callback?

Comments

tce’s picture

I think you can use limit_validation_errors().

nashkrammer’s picture

Refer this https://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_...

$form['actions']['previous'] = array(
    '#type' => 'submit',
    '#value' => t('Previous'),
    '#limit_validation_errors' => array(),       // No validation.
    '#submit' => array('some_submit_function'),  // #submit required.
  );

adding #limit_validation_errors will not validate the form and discard the values

tamnv’s picture

It worked for me, thanks you

hugronaphor’s picture

Just don't forget that by setting '#limit_validation_errors' => array(), your $form_state['values'] are not going to be populated anymore.

Virang’s picture

Hi tce,

Thanks for this hint, it's working perfect. You saved my time.

a77icu5’s picture

I have a radio with an ajax callback that displays some fields according to the selected option but the fields are getting validated on the callback, any ideas?

$form['foo'] = array(
    '#type' => 'radios'
    '#name' => 'bar'
    '#options' => array(
        'foo' => 'bar'
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
        'callback' => '_foo_ajax_callback'
        'wrapper' => 'wrapper'
        'method' => 'replace'
        'effect' => 'fade'
    )
);
jdesrig’s picture

As explained in the documentation, this only applies to button, image, image_button and submit.
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...