【发布时间】:2017-08-31 11:08:01
【问题描述】:
我正在尝试从 drupal 表单中获取代码的价值。当它提交表单时。我将对数据进行验证。
第一个
**
* 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');
}
}
然后在age_validate函数中,我尝试打印出值,但是发现 在 $form_state 中,存在 profile_date 数据,但是我在 $element 中找不到数据。
函数如下:
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);
这是 $form_state 的打印内容的摘录。
[values] => Array
(
[profile_date] => Array
(
[day] => 31
[month] => 8
[year] => 2001
)
)
[input] => Array
(
[profile_date] => Array
(
[day] => 31
[month] => 8
[year] => 2001
)
)
您可以看到 profile_date 的值在输入中,值在此处
当我尝试这样做时
print_r($element);
结果在这里
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
)
在$element中看不到profile_date,但在$form_state中可以看到,并且$element数据重复,可能是什么原因导致数据丢失?
【问题讨论】: