0

Drupal form api reference example for #element_validate accesses element's value with $element['#value'] in field validation function:

function myelement_validate($element, &$form_state, $form) {
   if (empty($element['#value'])) {
     form_error($element, t('This field is required.'));
   }
}

But $element['#value'] is always empty for me. indeed in print_r($element) there is no key named #value in first dimension of the array. instead, it is in somewhere like $element['und'][0]['#entity']....

My code:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
   if (isset($form['field_myfield'])) {
      $form['field_myfield']['#element_validate'][] = 'myfield_validate';

   }
}

function myfield_validate($element, &$form_state, $form) {

   //print $element['#value']; exit; // returns nothing

   if (!empty($element['#value']) && !is_numeric($element['#value'])) { // always false
      form_error($element, t('Error'));
   }
}

Is anything wrong?

1
  • 2
    You can get values from the $form_state argument of the validation function.
    – harshal
    Sep 10 '13 at 5:15
1

Get a value using $form_state like as follow

$form_state['values']['field_myfield'];

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.