我想從drupal表單中獲取代碼的值。它在提交表單時。我會對數據進行驗證。驗證函數中缺少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數據是存在的,但是,我無法找到$數據元件。
下面是函數:
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
)
我無法看到$元素中的profile_date,但可以在$ form_state中看到它,並且$元素數據被複制,wha t是否會導致數據丟失的可能原因?
我可以看到profile_date是有[#value] => [day] => 31 [month] => 8 [year] => 2001 ),這是正確的輸入值。但是,當我嘗試打印$ element ['value'] ['year'],$ element ['value'] ['month'],$ element ['value'] ['day']時,空值。我該如何解決它? –