0

I am using mymodule_webform_submission_presave() to alter some of the data before it gets saved. I want to check if a certain select field has a certain option selected, dont save the form to the db and give the user an error like "you may not pick this" and dont save the form.

I would remove the option itself from the form but this wont be possible.

I've also tried to do it via the webform gui but there is no rule which says "this select may not have this option selected", is there?

Thanks!

2 Answers 2

2

Create your own validation function of your webform and do it there:

function modulename_form_webform_client_form_FORMID_alter(&$form, &$form_state, $form_id) {
     //FORMID - your webform id
    $form['actions']['submit']['#validate'][] = 'modulename_form_webform_client_form_FORMID_validate';

}

function modulename_form_webform_client_form_FORMID_validate($form, &$form_state) {
    //validate it here
    if ($form_state['values']['yourvalue'] == 'somevalue') {
        form_set_error('you can\t do that');
    }
}

also you can use hook_form_alter to provide your own validate function. Further reading form_set_error, hook_form_alter,

1

You would have to hook_form_alter your form and add your own validation function like:

function yourmodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'YOUR-FORMS-ID') {
   // ADD YOUR OWN VALIDATOR TO THE VALIDATORS ARRAY
   // use devel module and dpm($form)
   $form['#validate'][] = '_my_module_validate';
  } 
}

then you could write the validator

function _my_module_validate(&$form, &$form_state) {
   if($form_state['values']['your-element'] == 'some-forbidden-value'){
     form_set_error('my-module-error','ERROR MESSAGE');
   }
}
1
  • Almost identical answers, keep in mind that hook_form_FORM_ID_alter is executed after hook_form_alter()
    – Pan Chrono
    Commented Feb 28, 2013 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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