Why is the drupal 6 status message not showing what fields are required when I first submit the form

StackOverflow https://stackoverflow.com/questions/7778884

  •  10-02-2021
  •  | 
  •  

문제

When I submit the form without filling in one of the required fields( or any combination of required fields) there is no status message presented to let me know I have missed the required fields. The second time I submit the form the status message shows me what fields are required. The status message seems to be one step behind the form submission.

If after the first submit I change what fields are filled in and I submit again then the status message that should have show the previous time will show now.

When the form is filled in correctly it submits as normal.

The form is displayed using drupal_get_form( 'otherWaysToRequest' ); This is called in a template file in the theme.

Does anyone know why the status message is one step behind?

This is a sample of the code being used

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
}
도움이 되었습니까?

해결책

It's because by the time you're calling drupal_get_form in your template file the messages have already been committed for the current page; your validation messages will therefore show up the next time messages are displayed to the screen which is on the next page load.

You should build up the form in a custom module rather than the theme to get around this. The easiest way would be to create a block which you can assign to a region (using either hook_block in Drupal 6 or a combination of hook_block_info() and hook_block_view in Drupal 7).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top