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?