3

I am using #state values in a hook_form_alter() to disable field_2 if field_1 (a checkbox) is checked. It works and all is good.

Now I want to empty field_2 as well. I cannot see a way to do that with the #state.

How can I make sure that values don't get saved for fields that are conditionally disabled, even if the user enters a value first?

0

Interesting question. Since the $form_state variable is passed by reference to all form validation & submit handlers, you can modify it prior to it reaching the submit handler that saves it. What about adding a new validation handler that will empty the $form_state['values']['field_2'] value if the field is disabled in $form? That should ensure that the field is empty by the time anything tries to save it.

| improve this answer | |
  • I don't know that the information about whether it was enabled or disabled at time of submit is stored into a variable. How would I examine this in the PHP? – beth Mar 6 '13 at 23:04
  • Whenever an AJAX event is triggered to update the form (such as when elements are enabled/disabled via the State framework), the form is rebuilt. Simply looking at the correct element in $form ($form['field_2'], perhaps), should give you the info you're after (this would be done in the validation callback). – Yuriy Babenko Mar 6 '13 at 23:07
  • I don't think it triggers an AJAX event, I think it's purely JS adding/removing classes on the elements. Is that wrong? – beth Mar 6 '13 at 23:42
  • Come to think of it, I have a feeling you're right - there's no AJAX call happening (I'm going by memory, no example in front of me). So then, could you not look at the value of field_1? It must be checked for field_2 to get disabled, so that means it'll have a value of 1 in $form_state['values']['field_1'], which will mean that field_2 must have been disabled and can be safely erased. – Yuriy Babenko Mar 6 '13 at 23:56
  • If, during an edit a field's value is removed, and then the field is in a disabled state at the time of form submission, the empty value of the field is not saved. Instead, Drupal thinks it still has its value from the previous save. Removing the value from field_1 to enable field_2, and then adding a value to field_2, results in both fields submitting with values, which is a validation error on my form. – beth Mar 7 '13 at 0:26
0

After a little digging, it seems there is no way to empty a field (except for a checkbox --- 'check' and 'uncheck' are supported by #states, but no other value changes) that is disabled. What I ended up doing was hiding the fields when other fields had value. That way, when the user removes the value and then enters a value in another field, the first field is saved as empty (instead of with its previous value) and the second field's value is saved normally.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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