1(function( factory ) { 2 if ( typeof define === "function" && define.amd ) { 3 define( ["jquery", "../jquery.validate"], factory ); 4 } else if (typeof module === "object" && module.exports) { 5 module.exports = factory( require( "jquery" ) ); 6 } else { 7 window.Validator = factory(jQuery); 8 } 9}(function( $ ) { 10 11 /** 12 * ãã©ã¼ã ã®ããªãã¼ã·ã§ã³ãåæåãããµã¼ãã¼å´ã®çµæãé©ç¨ãã 13 * @param {HTMLFormElement|jQuery} formEl 対象ãã©ã¼ã ã®DOMã¾ãã¯jQueryãªãã¸ã§ã¯ã 14 * @param {Object} rules jQuery Validateã®ã«ã¼ã« 15 * @param {Object} messages jQuery Validateã®ã¡ãã»ã¼ã¸ 16 * @param {Object} serverResults ãµã¼ãã¼å´ã®ããªãã¼ã·ã§ã³çµæ (true/false) 17 */ 18function initFormValidation(formEl, rules, messages, serverResults) { 19 const $form = $(formEl); 20 21 // jQuery Validateã®ããã©ã«ãè¨å® 22 $.validator.setDefaults({ 23 errorClass: "custom-error", // ã¨ã©ã¼æã«è¿½å ããCSSã¯ã©ã¹ 24 errorElement: "p", // ã¨ã©ã¼è¡¨ç¤ºã®ã¿ã° 25 highlight: el => $(el).addClass("input-error"), // ã¨ã©ã¼æã«ãã©ã¼ã è¦ç´ ã«ã¯ã©ã¹è¿½å 26 unhighlight: el => $(el).removeClass("input-error"), // ã¨ã©ã¼è§£é¤æã«ã¯ã©ã¹åé¤ 27 errorPlacement: (error, el) => error.insertAfter(el) // ã¨ã©ã¼è¦ç´ ã®æ¿å ¥ä½ç½® 28 }); 29 // ãã©ã¼ã ã«ããªãã¼ã·ã§ã³ãé©ç¨ 30 const validator = $form.validate({ rules, messages }); 31 // ãµã¼ãã¼å´ã®ããªãã¼ã·ã§ã³çµæããã©ã¼ã ã«åæ 32 applyServerValidation(validator, messages, serverResults); 33} 34 35/** 36 * ãµã¼ãã¼å´ã®ããªãã¼ã·ã§ã³çµæããã©ã¼ã ã«é©ç¨ãã 37 * @param {Object} validator jQuery Validateã®ã¤ã³ã¹ã¿ã³ã¹ 38 * @param {Object} messages å端ã®ã¡ãã»ã¼ã¸ 39 * @param {Object} results ãµã¼ãã¼å´ã®çµæ { fieldName: { ruleName: true/false } } 40 */ 41function applyServerValidation(validator, messages, results) { 42 if (!results || Object.keys(results).length === 0) return; 43 const showErrors = {}; 44 45 // ãµã¼ãã¼çµæã1ã¤ãã¤ãã§ã㯠46 for (const field in results) { 47 for (const rule in results[field]) { 48 if (!results[field][rule]) { 49 // æå®ã®ã¡ãã»ã¼ã¸ãããã°ä½¿ç¨ããªããã°ããã©ã«ã 50 const msg = messages[field]?.[rule] || 'å ¥åãæ£ããããã¾ãã'; 51 showErrors[field] = showErrors[field] ? showErrors[field] + "ã" + msg : msg; 52 } 53 } 54 } 55 // jQuery Validateã«ã¨ã©ã¼ã表示ããã 56 validator.showErrors(showErrors); 57} 58 59// API return 60return { initFormValidation, applyServerValidation }; 61 62}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.