0

I have a webform and I need to validate a condition before you type in the database.

Use the hook_form_alter, this is my code:

function validate_entry_form_alter(&$form, $form_state, $form_id){
  if($form_id == 'webform_client_form_1'){
    $form['#submit'][] = "_validate_entry_webform_client_form_submit_handler";
  }
}

/* function HOOK_FORM_SUBMIT */

function _validate_entry_webform_client_form_submit_handler($form, &$form_state){
  $option = $form['submitted']['eventos']['#value'];
  $data_total = db_query("SELECT count(data) FROM webform_submitted_data WHERE data = $option")->fetchField();

  print_r('<pre>');
  print_r($data_total);
  print_r('</pre>');
  exit();
}

I've noticed that my function to handle the submit is handled when it wrote in database.

I need a "hook" where you can get all the results before they are written to database. Validate the data and if everything is correct, follow the workflow in database insert.

0

Form validation is called in the the first part af your code not in the submit function. Add another parameter to your form_alter:

$form['#validate'][] = 'nameOfMyModule_functionName_validate'; 
$form['#submit'][] = "_validate_entry_webform_client_form_submit_handler";

When validation passes, the submit function will be called. if not form_set_error can be called displaying a message like so:

function nameOfMyModule_functionName_validate($form, &$form_state) {
// Check for the color 'blue'.
    if ($form_state['values']['color'] == 'blue') {
    form_set_error('color', 'Sorry, your favorite color is actually yellow.');
    }
}
  • after implementing my validation function. If it passes validations How do I submit the workflow to continue normally? And if it fails How do I stop and only display a message? – Dvex Oct 23 '13 at 5:56
  • @Dvex I updated my answer to show you how this could be done. – jeroen Oct 23 '13 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.