1

I have a problem using #limit_validation_errors for a submit button on which I dont want all the validations to run. Here's my custom submit button :

$form['actions']['draft'] = array(
     '#type' => 'submit',
     '#value' => 'Save as draft',
     '#submit' => array(''),
     '#limit_validation_errors' => array(
       array('title'), // Validate $form_state['values']['title'].
     ),
);

It works perfectly in the sense that it checks the validation on the title field only, but the weird thing is, it does not submit any other field's values when submitted. I wrote a custom submit handler to check that. However, the values are present in $form_state['input'] but not in $form_state['values'].

What I want simply is, this form will submit with just the values given and overlook the validations when this custom submit button is clicked.

FYI : I can get $form_state['values'] perfectly in my custom validate function.

Any ideas? What am I doing wrong here?

2 Answers 2

1

It works as expected - in $form_state['values'] you have validated values, and in $form_state['input'] you have raw values. If you will limit validation to title field only - only this field will be transferred into "values" array.

0

I had to switch from a type="submit" to type="button" in order to disable errors + use ajax.

  public function buildForm(array $form, FormStateInterface $form_state) {

    // ...

    // Create Draft Button
    $form['#prefix'] = '<div id="send-emails-manual-send-by-role-wrapper">';
    $form['#suffix'] = '</div>';
    $form['actions']['save_draft'] = [
      '#type' => 'button',
      '#value' => $this->t('Save Draft'),
      '#name' => 'submit__save_draft', // TODO: Make constant
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => [$this, 'ajaxSubmitForm'],
        'wrapper' => 'send-emails-manual-send-by-role-wrapper',
        'method' => 'replace',
        'effect' => 'fade',
      ],
    ];
    
    return $form;  
  }

  /**
   * Submit the form using ajax
   */
  public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
    $this->submitForm($form, $form_state);
    return $form;
  }
  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.