vendor/assets/javascripts/webshims/shims/form-validators.js in webshims-rails-1.12.2 vs vendor/assets/javascripts/webshims/shims/form-validators.js in webshims-rails-1.12.3

- old
+ new

@@ -40,54 +40,52 @@ ; } }; webshims.refreshCustomValidityRules = function(elem){ if(!initTest){return;} - var val; - var data = $(elem).data() || $.data(elem, {}); - var customMismatchedRule = data.customMismatchedRule; - var validity = $.prop(elem, 'validity') || {}; + var val, setMessage; + var data = $(elem).data(); var message = ''; - var setMessage = function(message, errorType){ - blockCustom = true; - - - if(message){ - data.customMismatchedRule = errorType; - - if(typeof message != 'string'){ - message = webshims.getContentValidationMessage(elem, false, errorType); - - if(message && typeof message == 'object'){ - message = message[errorType]; + var customMismatchedRule = data && data.customMismatchedRule; + var validity = data && $.prop(elem, 'validity') || {valid: 1}; + + if(data && (customMismatchedRule || validity.valid)){ + setMessage = function(message, errorType){ + blockCustom = true; + + if(message){ + data.customMismatchedRule = errorType; + + if(typeof message != 'string'){ + message = webshims.getContentValidationMessage(elem, false, errorType); + + if(message && typeof message == 'object'){ + message = message[errorType]; + } + + if(!message || typeof message != 'string'){ + message = webshims.customErrorMessages[errorType][webshims.activeLang()] || webshims.customErrorMessages[errorType][''] || message.customError || message.defaultMessage || ''; + } } - - if(!message || typeof message != 'string'){ - message = webshims.customErrorMessages[errorType][webshims.activeLang()] || webshims.customErrorMessages[errorType][''] || message.customError || message.defaultMessage || ''; - } + } else { + message = ''; + data.customMismatchedRule = ''; } - } else { - message = ''; - data.customMismatchedRule = ''; - } - - - - $(elem).setCustomValidity(message); - blockCustom = false; - }; - if(customMismatchedRule || validity.valid){ + + $(elem).setCustomValidity(message); + blockCustom = false; + }; val = $(elem).val(); $.each(customValidityRules, function(name, test){ message = test(elem, val, data, setMessage) || ''; customMismatchedRule = name; if(message){ return false; } }); - if(data.dependentValidation && !data.dependentValidation._init && !data.dependentValidation.masterElement){ + if(data && data.dependentValidation && !data.dependentValidation._init && !data.dependentValidation.masterElement){ customValidityRules.dependent(elem, val, data, $.noop); } if(message != 'async' && (message || !validity.valid)){ setMessage(message, customMismatchedRule); } @@ -128,21 +126,21 @@ })(); /* * adds support for HTML5 constraint validation * - partial pattern: <input data-partial-pattern="RegExp" /> - * - creditcard-validation: <input class="creditcard-input" /> + * - creditcard-validation: <input data-creditcard="" /> * - several dependent-validation patterns (examples): * - <input type="email" id="mail" /> <input data-dependent-validation='mail' /> * - <input type="date" id="start" data-dependent-validation='{"from": "end", "prop": "max"}' /> <input type="date" id="end" data-dependent-validation='{"from": "start", "prop": "min"}' /> * - <input type="checkbox" id="check" /> <input data-dependent-validation='checkbox' /> */ (function(){ var formCFG = webshims.cfg.forms; var addCustomValidityRule = webshims.addCustomValidityRule; var getId = function(name){ - return document.getElementById(name); + return document.getElementById(name) || document.getElementsByName(name); }; addCustomValidityRule('partialPattern', function(elem, val, pattern){ pattern = pattern.partialPattern; if(!val || !pattern){return;} return !(new RegExp('(' + pattern + ')', 'i').test(val)); @@ -155,38 +153,34 @@ if($.nodeName(elem, 'input')){ webshims.warn('depreacated data-minlength usage: Use pattern=".{'+ data.minlength +'3,}" instead.'); } return data.minlength > val.length; }, 'Entered value is too short.'); - - var groupTimer = {}; - addCustomValidityRule('group-required', function(elem, val){ + + addCustomValidityRule('grouprequired', function(elem, val, data){ + var form; var name = elem.name; - if(!name || elem.type !== 'checkbox' || !$(elem).hasClass('group-required')){return;} - var checkboxes = $( (elem.form && elem.form[name]) || document.getElementsByName(name)); - var isValid = checkboxes.filter(':checked:enabled'); - if(groupTimer[name]){ - clearTimeout(groupTimer[name]); - } - groupTimer[name] = setTimeout(function(){ - checkboxes - .addClass('group-required') + if(!name || elem.type !== 'checkbox' || !('grouprequired' in data)){return;} + + if(!data.grouprequired || !data.grouprequired.checkboxes){ + data.grouprequired.checkboxes = $( ((form = $.prop(elem, 'form')) && form[name]) || document.getElementsByName(name)).filter('[type="checkbox"]'); + data.grouprequired.checkboxes .off('click.groupRequired') .on('click.groupRequired', function(){ - checkboxes.filter('.group-required').each(function(){ - webshims.refreshCustomValidityRules(this); - }); + webshims.refreshCustomValidityRules(elem); }) ; - }, 9); + data.grouprequired.checkboxes.not(elem).removeData('grouprequired'); + } + - return !(isValid[0]); + return !(data.grouprequired.checkboxes.filter(':checked:enabled')[0]); }, 'Please check one of these checkboxes.'); // based on https://sites.google.com/site/abapexamples/javascript/luhn-validation - addCustomValidityRule('creditcard', function(elem, value){ - if(!value || !$(elem).hasClass('creditcard-input')){return;} + addCustomValidityRule('creditcard', function(elem, value, data){ + if(!value || (!data || !('creditcard' in data))){return;} value = value.replace(/\-/g, ""); //if it's not numeric return true >- for invalid if(value != value * 1){return true;} var len = value.length; var sum = 0; @@ -265,15 +259,21 @@ } data = $.data(elem, 'dependentValidation', $.extend({_init: true}, dependentDefaults, data)); if(data.prop !== "value" || specialVal){ - $(data.masterElement.type === 'radio' && getGroupElements(data.masterElement) || data.masterElement).bind('change', depFn); + $(data.masterElement.type === 'radio' && getGroupElements(data.masterElement) || data.masterElement).on('change', depFn); } else { - $(data.masterElement).bind('change', function(){ - webshims.refreshCustomValidityRules(elem); - $(elem).getShadowElement().filter('.user-error, .user-success').trigger('refreshvalidityui'); + $(data.masterElement).on('change', function(){ + $(elem) + .getShadowElement() + .filter('.user-error, .user-success') + .each(function(){ + webshims.refreshCustomValidityRules(elem); + }) + .trigger('refreshvalidityui') + ; }); } } if(data.prop == "value" && !specialVal){ @@ -292,10 +292,13 @@ var opts; if(!data.remoteValidate){ if(typeof data.ajaxvalidate == 'string'){ data.ajaxvalidate = {url: data.ajaxvalidate, depends: $([])}; } else { - data.ajaxvalidate.depends = data.ajaxvalidate.depends ? $(data.ajaxvalidate.depends).map(getId) : $([]); + data.ajaxvalidate.depends = data.ajaxvalidate.depends ? + $(typeof data.ajaxvalidate.depends == 'string' && data.ajaxvalidate.depends.split(' ') || data.ajaxvalidate.depends).map(getId) : + $([]) + ; } data.ajaxvalidate.depends.on('refreshCustomValidityRules', function(){ webshims.refreshCustomValidityRules(elem); });