PluginProbe
Advanced Custom Fields (ACF®) / 4.4.11
Advanced Custom Fields (ACF®) v4.4.11
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / js / input / validation.js
validation.js
432 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($){
2
3
4 /*
5 * Validation
6 *
7 * JS model
8 *
9 * @type object
10 * @date 1/06/13
11 *
12 */
13
14 acf.validation = {
15
16 status : true,
17 disabled : false,
18
19 run : function(){
20
21 // reference
22 var _this = this;
23
24
25 // reset
26 _this.status = true;
27
28
29 // loop through all fields
30 $('.field.required, .form-field.required').each(function(){
31
32 // run validation
33 _this.validate( $(this) );
34
35
36 });
37 // end loop through all fields
38 },
39
40 /*
41 * show_spinner
42 *
43 * This function will show a spinner element. Logic changed in WP 4.2
44 *
45 * @type function
46 * @date 3/05/2015
47 * @since 5.2.3
48 *
49 * @param $spinner (jQuery)
50 * @return n/a
51 */
52
53 show_spinner: function( $spinner ){
54
55 // bail early if no spinner
56 if( !$spinner.exists() ) {
57
58 return;
59
60 }
61
62
63 // vars
64 var wp_version = acf.o.wp_version;
65
66
67 // show
68 if( parseFloat(wp_version) >= 4.2 ) {
69
70 $spinner.addClass('is-active');
71
72 } else {
73
74 $spinner.css('display', 'inline-block');
75
76 }
77
78 },
79
80
81 /*
82 * hide_spinner
83 *
84 * This function will hide a spinner element. Logic changed in WP 4.2
85 *
86 * @type function
87 * @date 3/05/2015
88 * @since 5.2.3
89 *
90 * @param $spinner (jQuery)
91 * @return n/a
92 */
93
94 hide_spinner: function( $spinner ){
95
96 // bail early if no spinner
97 if( !$spinner.exists() ) {
98
99 return;
100
101 }
102
103
104 // vars
105 var wp_version = acf.o.wp_version;
106
107
108 // hide
109 if( parseFloat(wp_version) >= 4.2 ) {
110
111 $spinner.removeClass('is-active');
112
113 } else {
114
115 $spinner.css('display', 'none');
116
117 }
118
119 },
120
121 validate : function( div ){
122
123 // var
124 var ignore = false,
125 $tab = null;
126
127
128 // set validation data
129 div.data('validation', true);
130
131
132 // not visible
133 if( div.is(':hidden') )
134 {
135 // ignore validation
136 ignore = true;
137
138
139 // if this field is hidden by a tab group, allow validation
140 if( div.hasClass('acf-tab_group-hide') )
141 {
142 ignore = false;
143
144
145 // vars
146 var $tab_field = div.prevAll('.field_type-tab:first'),
147 $tab_group = div.prevAll('.acf-tab-wrap:first');
148
149
150 // if the tab itself is hidden, bypass validation
151 if( $tab_field.hasClass('acf-conditional_logic-hide') )
152 {
153 ignore = true;
154 }
155 else
156 {
157 // activate this tab as it holds hidden required field!
158 $tab = $tab_group.find('.acf-tab-button[data-key="' + $tab_field.attr('data-field_key') + '"]');
159 }
160 }
161 }
162
163
164 // if is hidden by conditional logic, ignore
165 if( div.hasClass('acf-conditional_logic-hide') )
166 {
167 ignore = true;
168 }
169
170
171 // if field group is hidden, igrnoe
172 if( div.closest('.postbox.acf-hidden').exists() ) {
173
174 ignore = true;
175
176 }
177
178
179 if( ignore )
180 {
181 return;
182 }
183
184
185
186 // text / textarea
187 if( div.find('input[type="text"], input[type="email"], input[type="number"], input[type="hidden"], textarea').val() == "" )
188 {
189 div.data('validation', false);
190 }
191
192
193 // wysiwyg
194 if( div.find('.acf_wysiwyg').exists() && typeof(tinyMCE) == "object")
195 {
196 div.data('validation', true);
197
198 var id = div.find('.wp-editor-area').attr('id'),
199 editor = tinyMCE.get( id );
200
201
202 if( editor && !editor.getContent() )
203 {
204 div.data('validation', false);
205 }
206 }
207
208
209 // select
210 if( div.find('select').exists() )
211 {
212 div.data('validation', true);
213
214 if( div.find('select').val() == "null" || ! div.find('select').val() )
215 {
216 div.data('validation', false);
217 }
218 }
219
220
221 // radio
222 if( div.find('input[type="radio"]').exists() )
223 {
224 div.data('validation', false);
225
226 if( div.find('input[type="radio"]:checked').exists() )
227 {
228 div.data('validation', true);
229 }
230 }
231
232
233 // checkbox
234 if( div.find('input[type="checkbox"]').exists() )
235 {
236 div.data('validation', false);
237
238 if( div.find('input[type="checkbox"]:checked').exists() )
239 {
240 div.data('validation', true);
241 }
242 }
243
244
245 // relationship
246 if( div.find('.acf_relationship').exists() )
247 {
248 div.data('validation', false);
249
250 if( div.find('.acf_relationship .relationship_right input').exists() )
251 {
252 div.data('validation', true);
253 }
254 }
255
256
257 // repeater
258 if( div.find('.repeater').exists() )
259 {
260 div.data('validation', false);
261
262 if( div.find('.repeater tr.row').exists() )
263 {
264 div.data('validation', true);
265 }
266 }
267
268
269 // gallery
270 if( div.find('.acf-gallery').exists() )
271 {
272 div.data('validation', false);
273
274 if( div.find('.acf-gallery .thumbnail').exists())
275 {
276 div.data('validation', true);
277 }
278 }
279
280
281 // hook for custom validation
282 $(document).trigger('acf/validate_field', [ div ] );
283
284
285 // set validation
286 if( ! div.data('validation') )
287 {
288 // show error
289 this.status = false;
290 div.closest('.field').addClass('error');
291
292
293 // custom validation message
294 if( div.data('validation_message') )
295 {
296 var $label = div.find('p.label:first'),
297 $message = null;
298
299
300 // remove old message
301 $label.children('.acf-error-message').remove();
302
303
304 $label.append( '<span class="acf-error-message"><i class="bit"></i>' + div.data('validation_message') + '</span>' );
305 }
306
307
308 // display field (curently hidden due to another tab being active)
309 if( $tab )
310 {
311 $tab.trigger('click');
312 }
313
314 }
315 }
316
317 };
318
319
320 /*
321 * Events
322 *
323 * Remove error class on focus
324 *
325 * @type function
326 * @date 1/03/2011
327 *
328 * @param N/A
329 * @return N/A
330 */
331
332 $(document).on('focus click', '.field.required input, .field.required textarea, .field.required select', function( e ){
333
334 $(this).closest('.field').removeClass('error');
335
336 });
337
338
339 /*
340 $(document).on('blur change', '.field.required input, .field.required textarea, .field.required select', function( e ){
341
342 acf.validation.validate( $(this).closest('.field') );
343
344 });
345 */
346
347
348 /*
349 * Save Post
350 *
351 * If user is saving a draft, allow them to bypass the validation
352 *
353 * @type function
354 * @date 1/03/2011
355 *
356 * @param N/A
357 * @return N/A
358 */
359
360 $(document).on('click', '#save-post', function(){
361
362 acf.validation.disabled = true;
363
364 });
365
366
367 /*
368 * Submit Post
369 *
370 * Run validation and return true|false accordingly
371 *
372 * @type function
373 * @date 1/03/2011
374 *
375 * @param N/A
376 * @return N/A
377 */
378
379 $(document).on('submit', '#post', function(){
380
381 // If disabled, bail early on the validation check
382 if( acf.validation.disabled )
383 {
384 return true;
385 }
386
387
388 // do validation
389 acf.validation.run();
390
391
392 if( ! acf.validation.status ) {
393
394 // vars
395 var $form = $(this);
396
397
398 // show message
399 $form.siblings('#message').remove();
400 $form.before('<div id="message" class="error"><p>' + acf.l10n.validation.error + '</p></div>');
401
402
403 // hide ajax stuff on submit button
404 if( $('#submitdiv').exists() ) {
405
406 // remove disabled classes
407 $('#submitdiv').find('.disabled').removeClass('disabled');
408 $('#submitdiv').find('.button-disabled').removeClass('button-disabled');
409 $('#submitdiv').find('.button-primary-disabled').removeClass('button-primary-disabled');
410
411
412 // remove spinner
413 acf.validation.hide_spinner( $('#submitdiv .spinner') );
414
415 }
416
417 return false;
418 }
419
420
421 // remove hidden postboxes
422 // + this will stop them from being posted to save
423 $('.acf_postbox.acf-hidden').remove();
424
425
426 // submit the form
427 return true;
428
429 });
430
431
432 })(jQuery);