drupaldrupal-webformform-api

check field and redirection on a multistep webform - Drupal


I have set up a multistep webform on a Drupal website and I am trying to check a field and stay on the first page of the webform if the field doesn't meet the conditions. On the first step of the webform, a user enters a company name in a textfield (comp_name). When the user pushes next, I want to check if the company name entered is a title of a node . So far I have:

function check_for_company_form_alter(&$form, $form_state, $form_id)
{
if ($form_id == "webform_client_form_2")
{   
    if($form_state['post']['details']['page_num'] == 1){
        $comp_name = $form_state['post']['submitted']['comp_name'];
        $query = "SELECT nid FROM node WHERE title='".$comp_name."'";
        $nidComp= db_result(db_query($query));
        if($nidComp>0){
        echo 'we found node id'.$nidComp;
        }
     else{
          //redirection to page 1 of the multistep form
     }
    }


}
} 

The code works well but I cannot find out how to redirect to page 1.


Solution

  • Use a custom validation hook and form_set_error.

    <?php
    
    ...
    // within hook_form_alter
    $form['#validate'][] = 'check_for_company_validate';
    ...
    
    function check_for_company_validate($form, &$form_state) {
      // Check if the company name ISN'T the title of a node here.
      // If so...
      form_set_error('post][submitted][comp_name', 'Something is not right.');
    }
    
    ?>
    

    hook_form_alter fires before a form is built. It sounds like you want hook_validate or (maybe) hook_submit. This solution is for Drupal 6.