0

I am altering a paragraphs form to add a validation function to the "Add More" button using this code: [This button add widgets for multiple value fields]

function mymodule_field_widget_paragraphs_form_alter(&$element, FormStateInterface &$form_state, $context) {
  if ($element['#paragraph_type']=='my_parag'){
    if (isset($element['subform']['my_parag_field']['widget']['add_more']['add_more_button_my_parag'])) {
      //Here is the code that adds the validate function
      $element['subform']['my_parag_field']['widget']['add_more']['add_more_button_my_parag']['#validate'] = ['validate_add_more_button'];
    }
  }
}

This handmade validation function is called but I couldn't stop the submit function to be called with $form_state->setError

function validate_add_more_button(array $form, FormStateInterface $form_state){
  //This line has no effect... a new widget is added
  $form_state->setError($form, t("error"));
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.