0

i have a custom form validate function. what is the correct/reliable way to get the field value and why? below two lines get the same value from a Select List.

a) $form['field_a']['und']['#value']

b) $form_state['values']['field_a']['und'][0]['value']

1 Answer 1

2

Refering to form API Drupal 7 for #value

$form['field_a']['und']['#value']

Used by: button, hidden, image_button, submit, token, value

Description: Used to set values that cannot be edited by the user.

For $form_state['values']['field_a']['und'][0]['value']

$form_state is one of the arguments passed to a form submission handler, or a form validation handler; its main usage is to retrieve the values entered from the user in the form see the content of $form_state['values'])

So you should use $form_state['values']['field_a']['und'][0]['value']

1
  • 3
    It would be recommended to use LANGUAGE_NONE constant instead of 'und'. Like this: $form_state['values']['field_a'][LANGUAGE_NONE][0]['value']. If you have translatable fields check this out: drupal.stackexchange.com/questions/25839/…
    – jiv-e
    Commented Dec 5, 2014 at 21:27

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.