I have an validate code:
public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $form_state->setValue('my_variable', $form_state->getValue('my_variable') + 1); $form_state->setRebuild(); }
And I previously set my_variable
to 1
if it’s NULL
:
public function buildForm(array $form, FormStateInterface $form_state) { if($form_state->getValue('my_variable') == NULL){ $form_state->setValue('my_variable', 1); } ...
I use ajax callback for form submission and the validation works fine. After click on submit button I display my_variable
and I see 2
(before submission it’s 1
).
But when I hit the submit button again I see 2
again, instead of 3
. Also, when I re-load the page the data is missing (so it’s get value 1
again).
How to prevail the value? It looks like I am operating on copy of $form_state
instead of reference but I cannot write &$form_state (public function validateForm(array &$form, FormStateInterface &$form_state)
) because it gives me an error (as it’s not the correct declaration of validateForm method).