Hi,

I‘ve written some custom validation as per here: https://www.drupal.org/node/908600

The problem is that my validation hook is also firing on the webform‘s preview page. However, the submitted values aren‘t populated. I‘m struggling to get round this one - there doesn‘t seem to be an easy way to check that I‘m on the preview page in form alter when the validation is added (or should this check even be in the form alter hook?)

function mymodule_form_alter(&$form, &$form_state, $form_id) {

  if ($form["#node"]->nid != SUBMIT_BUSINESS_WEBFORM_ID) {
    return;
  }

  // THE BELOW DOES NOT WORK TO PREVENT VALIDATION FIRING ON PREVIEW PAGE
  if ($form_state['values']['details']['page_num'] == $form_state['values']['details']['page_count']) {
    return;
  }
  
  $form['#validate'][] = 'mymodule_form_validate';
}

function mymodule_form_validate($form, &$form_state) {
  // DOES NOT WORK AS values/submitted are not populated on preview page

  // Check country is set.  
  if ($form_state['values']["submitted"]['country'] == UNSELECTED_COUNTRY_CODE) {
    form_set_error('country', 'Please select a country.');
  }
  
  // Check geocode works
  if (FALSE == mymodule_value_check($form_state["values"]["submitted"])) {
    form_set_error('street_address', 'Could not interpret address - please correct it and try again.');
  }
}

Comments

CiviFirst John’s picture

v 7 btw :)

Jaypan’s picture

You can try this. You may need to change $form_state['values']['preview'] - I haven't checked if that's correct.

function mymodule_form_validate($form, &$form_state) {
  if($form_state['values']['op'] != $form_state['values']['preview']) {

    // Check country is set.  
    if ($form_state['values']["submitted"]['country'] == UNSELECTED_COUNTRY_CODE) {
      form_set_error('country', 'Please select a country.');
    }
  
    // Check geocode works
    if (FALSE == mymodule_value_check($form_state["values"]["submitted"])) {
      form_set_error('street_address', 'Could not interpret address - please correct it and try again.');
    }
  }
}
CiviFirst John’s picture

Thanks Jaypan for your response. That helped me get to my eventual solution, which is to check the $form_state['values']['op'] attribute. If it‘s "Submit" then I know I‘m on the preview page :)