1 | <?php |
---|
2 | // $Id: skip_validation.module,v 1.5 2009/10/13 04:17:56 cdale Exp $ |
---|
3 | |
---|
4 | /** |
---|
5 | * @file |
---|
6 | * |
---|
7 | * Provides an API which allows developers to skip validation procedures on select submit buttons. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Implementation of hook_help(). |
---|
12 | */ |
---|
13 | function skip_validation_help($path, $arg) { |
---|
14 | switch ($path) { |
---|
15 | case 'admin/help#skip_validation': |
---|
16 | return '<p>'. t('Provides an API which allows developers to skip validation procedures on select submit buttons.') .'</p>'; |
---|
17 | break; |
---|
18 | } |
---|
19 | } |
---|
20 | |
---|
21 | /** |
---|
22 | * Implementation of hook_elements(). |
---|
23 | */ |
---|
24 | function skip_validation_elements() { |
---|
25 | return array('form' => array('#after_build' => array('skip_validation_after_build'))); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * After build callback which handles processing of skip validation options. |
---|
30 | */ |
---|
31 | function skip_validation_after_build($form, &$form_state) { |
---|
32 | if (!empty($form_state['submitted'])) { |
---|
33 | if (!empty($form_state['clicked_button']['#skip_validation'])) { |
---|
34 | // Recursively set all children as validated. |
---|
35 | _skip_validation_skip_validation($form, 'full'); |
---|
36 | } |
---|
37 | elseif (!empty($form_state['clicked_button']['#skip_core_validation'])) { |
---|
38 | // Recursively remove #needs_validation from all children. |
---|
39 | _skip_validation_skip_validation($form, 'core'); |
---|
40 | } |
---|
41 | |
---|
42 | // Check for skip required validations. |
---|
43 | if (!empty($form_state['clicked_button']['#skip_required_validation'])) { |
---|
44 | // Recursively remove #required from all children. |
---|
45 | _skip_validation_skip_validation($form, 'required'); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return $form; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Recursive helper function to set the validated property. |
---|
54 | * |
---|
55 | * @param &$elements |
---|
56 | * The elements that are currently being processed. |
---|
57 | * @param $type |
---|
58 | * The type of validation to skip. |
---|
59 | * Accepted values are: |
---|
60 | * o full - Skips all validation, which includes core (see below) |
---|
61 | * and all #validate and #element_validate callbacks. |
---|
62 | * o core - Skips core validation routines, which includes |
---|
63 | * #required, #maxlength, and #options checking. |
---|
64 | * o required - Skips #required validations. |
---|
65 | */ |
---|
66 | function _skip_validation_skip_validation(&$elements, $type = '') { |
---|
67 | switch ($type) { |
---|
68 | case 'full': |
---|
69 | $elements['#validated'] = TRUE; |
---|
70 | break; |
---|
71 | case 'core': |
---|
72 | unset($elements['#needs_validation']); |
---|
73 | break; |
---|
74 | case 'required': |
---|
75 | // Set a pre render hook so we can tell the |
---|
76 | // theme to reset the required option for |
---|
77 | // rendering on validation errors. |
---|
78 | if (!empty($elements['#required'])) { |
---|
79 | $elements['#pre_render'][] = 'skip_validation_pre_render'; |
---|
80 | } |
---|
81 | |
---|
82 | unset($elements['#required']); |
---|
83 | break; |
---|
84 | default: |
---|
85 | return; |
---|
86 | } |
---|
87 | foreach (element_children($elements) as $key) { |
---|
88 | _skip_validation_skip_validation($elements[$key], $type); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Pre render callback to correct required validation |
---|
94 | * skipping on validation errors. This only corrects |
---|
95 | * the theme layer. Thankfully, the #required option |
---|
96 | * appears to be reset on form submission. |
---|
97 | */ |
---|
98 | function skip_validation_pre_render($element) { |
---|
99 | $element['#required'] = TRUE; |
---|
100 | return $element; |
---|
101 | } |
---|