为什么drupal 6状态消息在我第一次提交表单时没有显示需要哪些字段

时间:2011-10-15 15:54:32

标签: drupal drupal-fapi

当我提交表单而未填写其中一个必填字段(或任何必填字段的组合)时,没有提供状态消息让我知道我错过了必填字段。 我第二次提交表单时,状态消息显示我需要哪些字段。 状态消息似乎是表单提交的一步。

如果在第一次提交后我更改了填写的字段并再次提交,那么现在将显示应显示上一次的状态消息。

当表格填写正确时,它会正常提交。

使用drupal_get_form('otherWaysToRequest')显示表单; 这在主题中的模板文件中调用。

有谁知道为什么状态信息落后一步?

这是正在使用的代码示例

function otherWaysToRequest(&$form_state)
{
    global $base_url;
    $pathToTheme = path_to_theme();

    $form['top-check'] = array(
        '#type'     => 'fieldset',
        '#attributes' => array('class' => 'checkboxes'),
    );

    $form['top-check']['gift'] = array(
        '#title'     => t('Included a gift'),
        '#type'      => 'checkbox',
        '#suffix'    => '<br />',
        '#required'  => false,
    );

    $form['top-check']['contact'] = array(
        '#title'     => t('I would like to speak to you'),
        '#type'      => 'checkbox',
        '#suffix'    => '<br />',
        '#required'  => false,
    );

    $form['name'] = array(
        '#title'     => t('Name'),
        '#type'      => 'textfield',
        '#required'  => true,
    );


    $form['email'] = array(
        '#title'     => t('Email Address'),
        '#type'      => 'textfield',
        '#required'  => true,
    );

    $form['bottom-check'] = array(
        '#type'     => 'fieldset',
        '#attributes' => array('class' => 'checkboxes'),
        '#description' => t('<p class="Items">If you have ...:</p><p class="Items">I have included .....</p>')
    );

    $form['bottom-check']['share'] = array(
        '#title'     => t('A Share'),
        '#type'      => 'checkbox',
        '#suffix'    => '<br />',
        '#required'  => FALSE,
    );

    $form['submit'] = array(
        '#type'         =>  'image_button',
        '#src'          =>  $pathToTheme.'/image.gif',
        '#value'        =>  t('Submit Form'),
    );
}

function otherWaysToRequest_validate($form, &$form_state)
{
    $mail_reg_ex     = '/[-a-zA-Z0-9._]+[@]{1}[-a-zA-Z0-9.]+[.]{1}[a-zA-Z]{2,4}/';

    if(!preg_match($mail_reg_ex, $form_state['values']['email']))
    {
        form_set_error('email', t('Invalid email address.'));
    }
    if( 0 == $form_state['values']['gift'] & 0 == $form_state['values']['contact'] )
    {
        form_set_error('gift', t('You must choose one of the first two options on the form'));
    }
}

function otherWaysToRequest_submit($form, &$form_state)
{
    //mail details
}

1 个答案:

答案 0 :(得分:0)

这是因为当您在模板文件中调用drupal_get_form时,已经为当前页面提交了消息;因此,验证消息将显示 next 时间消息显示在下一页加载的屏幕上。

您应该在自定义模块而不是主题中构建表单来解决此问题。最简单的方法是创建一个可以分配给区域的块(使用Drupal 6中的hook_block或Drupal 7中hook_block_info()hook_block_view的组合。)