0

不确定 Drupal7 模板文件中使用的以下表单中的问题出在哪里。高度赞赏帮助。问题如下:
1. 变量 $title 和 $surname 没有传递给表单的默认值。
=> 错误消息:注意:未定义变量:form_user_information() 中的标题
=> 错误消息:注意:未定义变量:form_user_information() 中
的姓氏 2. 有一个警告:strpos() 期望参数 1 为字符串,数组在 drupal_strip_dangerous_protocols 中给出()

提前致谢。

   <?php
        //Load User data:
            global $user;
            $uid = $user->uid;
            $account = user_load($uid); 

        //Get User data:
            $title = 'Mrs.';
                print $title . '<br><br>'; //Result: Value is printed and not empty!        
            $surname = check_plain($account->field_vorname['und']['0']['value']);
            //$surname = 'Tom';
                print $surname . '<br><br>'; //Result: Both values are printed and are not empty!



        function form_user_information($form, &$form_state) {


        //Form
            $form['#action'][] = request_uri();
            $form['#id'][] = 'form_user_information';
            $form['#validate'][] = 'form_user_information_validators';
            $form['#submit'][] = 'form_user_information_submit';
            $form['#prefix'] = '<div id="form_user_information">';
            $form['#suffix'] = '</div>';

            //Select-Field: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#select
            $form['Title'] = array(
                '#type' => 'select',
                '#title' => t('Title'),
                '#options' => array(
                  'Frau' => t('Mr.'),
                  'Herr' => t('Mrs.'),
                ),
                '#default_value' => $title,
            );  

            $form['surname'] = array(
                '#type' => 'textfield',
                '#maxlength' => 50,
                '#size' => 40,
                '#required' => TRUE,
                '#title' => t('Surname'),
                //'#attributes' => array('placeholder' => $surname),
                '#default_value' => $surname,
            );

            $form['actions'] = array('#type' => 'actions');
            $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Confirm data');

        return $form;
        }


        //print form
            $form = drupal_get_form('form_user_information');
            print drupal_render($form);

        //Form Validation:
        function form_user_information_validators($form, &$form_state) {
          if ($form_state['values']['surname'] == '') {
            form_set_error('surname', t('Please enter your surname.'));
          }
        }

        //Form Submit:
        function form_user_information_submit($form, &$form_state) {

            //...

        }

    //get form information
        echo "<pre>".print_r($form,true)."</pre>";  

    ?>
4

2 回答 2

0

1) 将全局变量 $title 和 $surname 设置为全局范围:

//Get User data:  
$global $title = ...  
$global $surname = ...

否则将所有这些变量(包括 $user)设置function form_user_information在最佳实践中。

我还建议不要使用 $title 作为变量名,因为它可能会导致 $page 变量的已定义 $title 出现问题。而是使用类似 $user_title 的东西。

2)这来自哪条线?

于 2013-07-23T13:28:27.630 回答
0

猜猜这个问题可以关闭。请参阅 2 条评论。感谢西奥多罗斯的帮助。

于 2013-07-23T20:08:49.070 回答