验证函数中缺少drupal表单元素

时间:2017-08-31 11:08:02

标签: php 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数据存在,但是,我找不到$ 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中看到它,并且$元素数据是重复的,可能导致数据丢失的可能原因是什么?

1 个答案:

答案 0 :(得分:0)

我不知道我是否理解了你的问题。但我认为存在误解。

在表单中,编写了关于#element_validate属性的api引用     写道:

A validation function for an element takes $element, $form_state, and $form as parameters:
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#element_validate

所以age_validate的第一个参数是profile_date本身。 从您的输出中可以看出$element['#default_value']$form_state['value']['profile_date']是等于的, 所以你提交了表格而没有改变profile_date输入的值。

注意:如果你想在age_validate中加入整个表格,你可以添加第三个参数:

function age_validate($element, &$form_state, $form) {...}

$form内,您可以看到profile_date元素