I need to track previous values of a few form elements on a form to detect when they change to invalidate some other options on the page. Essentially, if the value of these fields change a field that controls pagination should reset.
I'm trying to accomplish this with this:
$values = $form_state->getValues();
$form['element1'] = [
'#type' => 'radios',
'#title' => $this->t('Element 1'),
'#options' => [
// Omitted for brevity
],
'#weight' => '0',
'#ajax' => [
'callback' => [$this, 'my_callback'],
'disable-refocus' => true
]
];
// dump($values['element1']);
$form['element1_previous'] = [
'#type' => 'value',
'#value' => (isset($values['element1'])) ? $values['element1'] : 'none',
];
No matter what I do the value of $values['element1_previous
] is none
. If I uncomment the dump()
statement right before it I can see the value of $values['element1']
changing each time AJAX is submitted so the value is there.
If I do something like:
$form['element1_previous'] = [
'#type' => 'value',
'#value' => rand()
];
I can see $values['element1_previous']
being set to a different integer each AJAX submission. I just can't assign the value of $form['element1']
to that field.
What am I doing wrong? Is there a better way I could handle this?