Drupal自定义表单记住文本字段数据

时间:2011-10-06 09:35:54

标签: php forms drupal module textfield

我正在为新模块设置配置页面,但我无法让我的文本字段记住我之前输入的数据。

我通过调用variable_get来设置#default_value,但是值永远不会在文本字段中重置。但是,它正确地存储为变量并在变量编辑器页面上可见。

下面是代码片段:

$form   =   array();

$form['username']   =   array(
    '#type'                     =>  'textfield',
    '#title'                    =>  t('Username'),
    '#default_value'            =>  variable_get('username', '' ),
    '#description'              =>  t('supply the username'),
    '#maxlength'                =>  30,
    '#required'                 =>  TRUE,
    '#weight'                   =>  -1,
);

return system_settings_form($form);

如果可以,请帮忙。

更新代码: 提交功能如下:

    function mymodule_settings_submit($form, &$form_state){
    drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
    variable_set( 'username', $form_state['values']['username'] );
}

更新的代码#2 完整代码:

    function my_module_init(){
}

function my_module_menu(){

    $items = array();
    $items['admin/settings/helper'] =   array(
        'title'                     =>  'helper',
        'page callback'             => 'drupal_get_form',
        'page arguments'            =>  array('helper_setup'),
        'access arguments'          =>  array('access administration pages'),
        'type'                      =>  MENU_NORMAL_ITEM,

    );
    return $items;

}

function helper_setup(){

    $node_types =   node_get_types('names');

    $form   =   array();

    $form['username']   =   array(
        '#type'                     =>  'textfield',
        '#title'                    =>  t('Username'),
        '#default_value'            =>  variable_get('username', '' ),
        '#description'              =>  t('supply the username'),
        '#maxlength'                =>  30,
        '#required'                 =>  TRUE,
        '#weight'                   =>  -1,
    );

    $form['#submit'][] =    'my_module_settings_submit';

    return system_settings_form($form);
}

function my_module_validate($form, &$form_state){
    die('validation area responding!');
}

function my_module_settings_submit($form, &$form_state){
    drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
    variable_set( 'username', $form_state['values']['username'] );
}

1 个答案:

答案 0 :(得分:1)

在您的表单提交功能中,您应插入variable_set('variableName' , 'value')变量未自动插入..

<强>更新

$form['username']   =   array(
    '#type'                     =>  'textfield',
    '#title'                    =>  t('Username'),
    '#default_value'            =>  variable_get('username', '' ),
    '#description'              =>  t('supply the username'),
    '#maxlength'                =>  30,
    '#required'                 =>  TRUE,
    '#weight'                   =>  -1,
);

$form['#submit'][] = '_myform_submit_function' ; 
return system_settings_form($form);

现在让我们构建你的提交功能

function _myform_submit_function($form,&$form_state ) {
variable_set('username', $form_state['values']['username']) ; 
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
}

现在清除缓存并告诉我它是否正常工作