4

How can I tell what values have changed in a validate hook of a form submission?

2 Answers 2

6

What I have done is to see if the value in $form_state['values'] is different from the #default_value in $form, like so:

if ( $form_state['values']['variable'] <> $form['variable']['#default_value'] ) { ... }
2
  • kinda what i said above, no?
    – au_stan
    May 8, 2012 at 17:16
  • Couldn't tell -- it seemed like only a flag to indicate that a change had occurred? I wanted more granularity.
    – user1359
    May 8, 2012 at 17:57
4

Something that i do in my forms is set defaults.

Take:

    isset($form_state['values']['datacenter']) ?
    $selected = $form_state['values']['datacenter'] : $selected = 0;

$form['grid']['datacenter'] = array(
    '#title'            => 'Datacenter',
    '#type'             => 'radios',
    '#default_value'    => $selected,
    '#required'         => true,
    '#options'          => array(),                     
);

what you could do is check your #default_value to see if its not 0 in this case. so in your MYMODULE_form_validate($form, &$form_state) function, simply grab your $form_state values and if its not zero in this case, call some other function or whatever you want to do.

Your Answer

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

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