不能确定Drupal7中的模板文件中使用了以下形式的问题。非常感谢帮助。问题如下:
1.变量$ title和$姓氏不会被传递到窗体的默认值。
=>错误消息:注意:未定义变量:在form_user_information标题()
=>错误消息:注意:未定义变量:在form_user_information()姓
2.有一个警告:strpos()预计参数1是字符串,在drupal_strip_dangerous_protocols()中给出的数组
在此先感谢。Forms =>变量不被接受
<?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>";
?>
- 把变量放在函数里面,但我不太明白为什么,坦率地说。 - $ title仅用于此论坛的翻译。 - 我的代码出错了。 \t更正信息: \t \t'Frau'=> t('Mr。'),必须是'Mr.' => t('Mr。')/ \t \t'Herr'=> t('Mrs。'),必须是'Mrs.' => t('夫人'), \t \t使默认值在选择列表中工作。 \t \t - 在重新加载站点时出现2)中的错误。 (array in drupal_strip_dangerous_protocols()(line 1350 of ... includes \ common.inc)。 似乎这是唯一持久的问题。感谢您的帮助至此,Theodoros –
2.中的警告/错误发生是因为下面一行:$ form ['#action'] [] = request_uri(); =>使用$ form ['#action'] = request_uri();相反,可能为$ form ['#id'] [] ='form_user_information'; –