0

Drupal 7. On form validate, I want to:

  • check the value of one of the form fields [field-1]
  • based on that value, completely remove another field [field-2] together with its values, from the form

If it makes any difference, [field-2] is a multiple-value entity-reference field.

I think I need to do something like --- get hold of $form_state['values']['field_2'] and set it so that it is in the same state it is before any data was entered by the user.

Am I on the right lines? I haven't been able to find examples or documentation on this.

EDIT:

This is what worked for me in the end:

In the submit function (not the validate function):

// Start with a clean array for the field we want to change.
$form_state['values']['field_2'] = array();
// Restore the array values that correspond to 'empty' for this field.
// In my case this was as follows:
$form_state['values']['field_2']['und'][0]['target_id'] = '';
1

Short answer is yes you are on the right track. Altering form values on validation can get a little tricky and complex depending on the form. You might want to check out form_set_value if you haven't already.

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.