2

I have form with static fields and dynamic fields as follow:

fieldset

  1. textfield00 (require = true)
  2. textfield01 (require = true)

fieldset

  1. textfield02

fieldset

  1. add textfield

  2. submit button

so, everytime add textfield pressed, it will add new/duplicate textfield02.

with add textfield code as follow:

$form['fields']['modules']['add'] = array(
      '#type' => 'submit',
      '#name' => 'addtextfield',
      '#value' => t('Add more TextField'),
      '#submit' => array(array($this, 'addTextfieldSubmit')),
      '#ajax' => array(
        'callback' => array($this, 'addTextFieldCallback'),
        'wrapper' => 'wrapper',
        'effect' => 'fade',
      ),
    );

the adding field is working fine, the only problem i have here is: because textfield00 and textfield01 is mandatory, if i pressed add button, it will send me an error message 'mandatory fields' before i can add new textfield.

so how can i 'bypass' the requried validation, everytime i press add textfiled and only validate 'required = true' when i press submit button.

Thank You, AnD

1
  • 1
    Add the #limit_validation_errors property to your ajax button.
    – Eyal
    Commented Jun 11, 2016 at 11:08

1 Answer 1

0

'#limit_validation_errors' => array();

$form['actions']['previous'] = array(
    '#type' => 'submit',
    '#value' => t('Previous'),
    '#limit_validation_errors' => array(
      // Validate $form_state->getValue('step1').
      array('step1'),
      // Validate $form_state->getValue(array('foo', 'bar')).
      array('foo', 'bar'),
    ),
    '#submit' => array('some_submit_function'), // #submit required.
  );

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.