I have a form built with the Form API that has multiple button
form elements. I don't want any of these to submit the form normally, I want them to utilize #ajax
and callbacks to update the display. I feel like I run into this on every so often and can't quite remember how to make it work.
When the form first loads, I want half of the buttons to be disabled
. When one of the buttons is clicked, I want to be able to detect this in buildForm
so I can set form defaults and remove the disable
property so other buttons can now be used.
I thought I could set storage in the callback and read it back the next time the form is built:
// Relevant bit of the form...
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
$form['actions']['#type'] = 'actions';
$form['actions']['update'] = [
'#type' => 'button',
'#value' => $this->t('Start Form'),
'#ajax' => [
'wrapper' => 'my-form-wrapper',
'callback' => [$this, 'update'],
'method' => 'replace',
]
];
$form['actions']['reset'] = [
'#type' => 'button',
'#value' => $this->t('Reset Form'),
'#ajax' => [
'wrapper' => 'my-form-wrapper',
'callback' => [$this, 'reset'],
'method' => 'replace',
],
'#disabled' => TRUE,
];
// The AJAX callback
public function update(array &$form, FormStateInterface $form_state) {
$form_state->set('flag_1', TRUE);
$form_state->set('flag_2', "Started!");
$form_state->setRebuild();
}
The callback fires, and I see $form_state
storage get updated with those arbitrary values, but when the form is rebuilt, I do not see them in $form_state
anymore.
I looked at a few other similar form API AJAX questions, but did not have any luck getting unstuck.
Note: I do not want to have to juggle this with jQuery. I want to be able to update form data behind the scenes and rebuild the form appropriately with the relevant data.