0

I inherited a website which contains a Webform made form to which I was asked to make some modifications.

The form contains two drop-down selection lists, one with predefined options, one who's content is dynamically filled in by making a request to a web-service based on the selected value of the first drop-down list.

Filling the second drop-down list is not the problem, but since it's marked as required this means that the form is also validated and that's where the error pops up: An illegal choice has been detected. Please contact the site administrator.

Now, since the content of the drop-down is dynamically generated the error itself makes sense as the selected value is compared to the available options. I want to disable the validation without removing the required flag from the form element.

I tried modifying the element options both via hook_form_alter() and a custom validate function, various combinations and I can see that the variable is set and contains the value, yet it is still validated.

$form['submitted']['ss_function']['#needs_validation'] = FALSE;
$form['submitted']['ss_function']['#webform_validated'] = TRUE;

How can I disable the actual form validation without removing the required flag from a Webform form?

1 Answer 1

3

After some more trial and error I found my mistake. The '#needs_validation' property of the element is set at a later stage than the hook_form_alter() call.

In my hook_form_alter() implementation I added a custom validation function in which I unset the property. The trick is to actually unset it, because it's only validated with an isset() call.

function mymodule_webform_client_form_FORMID_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['#validate'], 'mymodule_validate_FORMID');
}

function mymodule_validate_FORMID(&$form, &$form_state) {
  unset($form['submitted']['FIELDNAME']['#needs_validation']);
}

Your Answer

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

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