seane
seane

Reputation: 599

Programmatically skip default CCK form validation

Background: In Drupal 6, I have a very long CCK form with many required fields. I also have a module that grants some users special permissions. One permission should allow the user to skip the required fields and still be able to submit the form. I would like to warn the user that they skipped some fields that (to non-privileged users) are required before allowing them to confirm submission.

Question: How do I skip the default validation for a CCK form? (specifically, required fields)

Previous research: I have already searched thoroughly for an answer to this. I am aware that I should be using hook_form_alter() and probably after_build(), as well. I have already tried to reset the validation using

$form['#validate'] = array();

however there was no change in the way the validation behaved (e.g. errors for required fields remained, no submission ocurred).

Upvotes: 3

Views: 790

Answers (2)

Clive
Clive

Reputation: 36955

Setting $form['#validate'] = array(); should work to a degree (any explicit validation handlers will no longer be run), but there's also the #element_validate key which can be added to most elements, and the #required flag which will set off the default form validation.

The easiest way I can think to remove those constraints is to recursively run through the form and unset the rogue values. Something like this:

function mymodule_unrequire_element(&$element) {
  if (isset($element['#required'])) {
    unset($element['#required']);
  }
  if (isset($element['#element_validate'])) {
    unset($element['#element_validate']);
  }

  foreach (element_children($element) as $child_element) {
    mymodule_unrequire_element($element[$child_element]);
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the_form_id') {
    mymodule_unrequire_element($form);
  }
}

That's completely untested but I think it'll do the trick :)

Upvotes: 2

shahinam
shahinam

Reputation: 680

The module http://drupal.org/project/skip_validation offer skip validation functionality. Looking at it may be helpful.

Upvotes: 0

Related Questions