Setting value of form element from $form_state not working 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-&gt;getValues();
$form['element1'] = [
      '#type' =&gt; 'radios',
      '#title' =&gt; $this-&gt;t('Element 1'),
      '#options' =&gt; [
        // Omitted for brevity
      ],
      '#weight' =&gt; '0',
      '#ajax' =&gt; [
        'callback' =&gt; [$this, 'my_callback'],
        'disable-refocus' =&gt; true
      ]
    ];

// dump($values['element1']);
$form['element1_previous'] = [
      '#type' =&gt; 'value',
      '#value' =&gt; (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' =&gt; 'value',
          '#value' =&gt; 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?
This:

(isset($values['element1']))


will always return TRUE, since the form element exists, and therefore a value is always passed to the submit hander, even when the value is empty.

You likely want this:

!empty($values['element1'])