0

It is a multipage Ajax form and I need to alter elements in a page based on form validation . This means, even if validation fails and the form stays on the same page, I need to visually alter that form page. Setting a field error with $form_state->setErrorByName is not sufficient for me so I'm exploring other options that I can't seem to find :)

I've tried using $form_state->set() and $form_state->setTemporary() but it seems the values set this way are only set if the validations passes. If validation fails, I get NULLs back in buildForm.

I've also tried storing to Drupal session, which works, but not as expected since my form is an Ajax form and the session variable would only be available on a second submit and would persist one step further than needed.

And I'm out of options.

Any suggestions?

PS I'm on Drupal 10.

1 Answer 1

1

You can set values in $form_state, the problem is the timeline when buildForm() is executed.

For a multistep form the form is only rebuilt if it is error-free. So the solution would be to not set an error but rebuild a different version of the form including your custom error messages by setting:

$form_state->setRebuild();
1
  • yes, this would work. Thank you!
    – Trudog
    Apr 30 at 5:17

Your Answer

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

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