validation.js
| 1 | /* |
| 2 | * Validation |
| 3 | * |
| 4 | * @description: |
| 5 | * @since: 3.5.8 |
| 6 | * @created: 17/01/13 |
| 7 | */ |
| 8 | |
| 9 | (function($){ |
| 10 | |
| 11 | |
| 12 | var _validation = acf.validation; |
| 13 | |
| 14 | |
| 15 | /* |
| 16 | * do_validation |
| 17 | * |
| 18 | * @description: checks fields for required input |
| 19 | * @created: 1/03/2011 |
| 20 | */ |
| 21 | |
| 22 | _validation.run = function(){ |
| 23 | |
| 24 | _validation.status = true; |
| 25 | |
| 26 | $('.postbox:not(.acf-hidden) .field.required, .form-field.required').each(function(){ |
| 27 | |
| 28 | // vars |
| 29 | var div = $(this); |
| 30 | |
| 31 | |
| 32 | // set validation data |
| 33 | div.data('validation', true); |
| 34 | |
| 35 | |
| 36 | // if is hidden by conditional logic, ignore |
| 37 | if( div.hasClass('acf-conditional_logic-hide') ) |
| 38 | { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | // text / textarea |
| 44 | if( div.find('input[type="text"], input[type="number"], input[type="hidden"], textarea').val() == "" ) |
| 45 | { |
| 46 | div.data('validation', false); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | // wysiwyg |
| 51 | if( div.find('.acf_wysiwyg').exists() && typeof(tinyMCE) == "object") |
| 52 | { |
| 53 | div.data('validation', true); |
| 54 | |
| 55 | var id = div.find('.wp-editor-area').attr('id'), |
| 56 | editor = tinyMCE.get( id ); |
| 57 | |
| 58 | |
| 59 | if( editor && !editor.getContent() ) |
| 60 | { |
| 61 | div.data('validation', false); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // select |
| 67 | if( div.find('select').exists() ) |
| 68 | { |
| 69 | div.data('validation', true); |
| 70 | |
| 71 | if( div.find('select').val() == "null" || ! div.find('select').val() ) |
| 72 | { |
| 73 | div.data('validation', false); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // checkbox |
| 79 | if( div.find('input[type="checkbox"]:checked').exists() ) |
| 80 | { |
| 81 | div.data('validation', true); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | // relationship |
| 86 | if( div.find('.acf_relationship').exists() ) |
| 87 | { |
| 88 | div.data('validation', false); |
| 89 | |
| 90 | if( div.find('.acf_relationship .relationship_right input').exists() ) |
| 91 | { |
| 92 | div.data('validation', true); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // repeater |
| 98 | if( div.find('.repeater').exists() ) |
| 99 | { |
| 100 | div.data('validation', false); |
| 101 | |
| 102 | if( div.find('.repeater tr.row').exists() ) |
| 103 | { |
| 104 | div.data('validation', true); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // flexible content |
| 110 | if( div.find('.acf_flexible_content').exists() ) |
| 111 | { |
| 112 | div.data('validation', false); |
| 113 | if( div.find('.acf_flexible_content .values table').exists() ) |
| 114 | { |
| 115 | div.data('validation', true); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // gallery |
| 121 | if( div.find('.acf-gallery').exists() ) |
| 122 | { |
| 123 | div.data('validation', false); |
| 124 | |
| 125 | if( div.find('.acf-gallery .thumbnail').exists()) |
| 126 | { |
| 127 | div.data('validation', true); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // hook for custom validation |
| 133 | $(document).trigger('acf/validate_field', div ); |
| 134 | |
| 135 | |
| 136 | // set validation |
| 137 | if( ! div.data('validation') ) |
| 138 | { |
| 139 | _validation.status = false; |
| 140 | div.closest('.field').addClass('error'); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | }); |
| 145 | |
| 146 | |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * Remove error class on focus |
| 152 | * |
| 153 | * @description: |
| 154 | * @since: 3.5.8 |
| 155 | * @created: 17/01/13 |
| 156 | */ |
| 157 | |
| 158 | // inputs / textareas |
| 159 | $('.field.required input, .field.required textarea, .field.required select').live('focus', function(){ |
| 160 | $(this).closest('.field').removeClass('error'); |
| 161 | }); |
| 162 | |
| 163 | // checkbox |
| 164 | $('.field.required input:checkbox').live('click', function(){ |
| 165 | $(this).closest('.field').removeClass('error'); |
| 166 | }); |
| 167 | |
| 168 | })(jQuery); |