I am trying to get value in code from drupal form. When it submit the form. I will do the validation on the data.
First in
**
* Implements hook_form_FORM_ID_alter() for user_register_form().
*/
function username_generator_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['#type'] = 'fieldset';
$form['account']['name']['#suffix'] = '</div>';
}
drupal_add_js('function random_area_wrapper(name) { jQuery("#random-area-wrapper").html(name).slideDown(); }', 'inline');
$form['Personal Information']['profile_date']['#default_value'] = array(
'day' => date('j'),
'month' => date('n'),
'year' => date('Y')
);
$form['Personal Information']['profile_date']['#after_build'] = array('__set_year_range');
if (!user_access('administer users')) {
$form['Personal Information']['profile_date']['#element_validate'] = array('age_validate');
}
}
Then in the function age_validate, I try to print out the value, but find that in $form_state , the profile_date data is exist, However, I cannot find the data in $element.
Here is the function:
function age_validate($element, &$form_state) {
$rid = autoassignrole_get_active_path_rid();
$rid = array_diff($rid, array(0));
$rid = reset ($rid);
if (_role_access($rid, 'allow kids registration' ) || empty($rid)) {
print_r($element);
print_r($form_state);
And this is the extract of the print out of $form_state.
[values] => Array
(
[profile_date] => Array
(
[day] => 31
[month] => 8
[year] => 2001
)
)
[input] => Array
(
[profile_date] => Array
(
[day] => 31
[month] => 8
[year] => 2001
)
)
You can see the values of profile_date is in input and values is here
How ever when I try to
print_r($element);
The result is here
Array
(
[#default_value] => Array
(
[day] => 31
[month] => 8
[year] => 2017
)
[#after_build] => Array
(
[0] => __set_year_range
)
[#element_validate] => Array
(
[0] => age_validate
)
[#tree] =>
[#parents] => Array
(
[0] => profile_date
)
[#array_parents] => Array
(
[0] => Personal Information
[1] => profile_date
)
[#weight] => 0
[#processed] =>
[#required] =>
[#attributes] => Array
(
)
[#title_display] => before
[#id] => edit-profile-date--2
[#sorted] => 1
[#validated] => 1
)
[#after_build_done] => 1
)
Array
(
[#default_value] => Array
(
[day] => 31
[month] => 8
[year] => 2017
)
[#after_build] => Array
(
[0] => __set_year_range
)
[#element_validate] => Array
(
[0] => age_validate
)
[#tree] =>
[#parents] => Array
(
[0] => profile_date
)
[#array_parents] => Array
(
[0] => Personal Information
[1] => profile_date
)
[#weight] => 0
[#processed] =>
[#required] =>
[#attributes] => Array
(
)
[#title_display] => before
[#id] => edit-profile-date--2
[#sorted] => 1
[#validated] => 1
)
[#after_build_done] => 1
)
I CANNOT see the profile_date in $element but can see it in $form_state, and the $element data is duplicated, what is the possible reason that can cause the missing of data?