Skip to content

Commit

Permalink
MAGETWO-32480: Merge validation rules
Browse files Browse the repository at this point in the history
- Merge validation rules for a frontend area
  • Loading branch information
Denys Rul committed Jan 19, 2015
1 parent 396e45b commit 01cb488
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require([
<script>
require([
"jquery",
"mage/validation"
"mage/backend/validation"
], function(jQuery){

//<![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
define([
"jquery",
"jquery/ui",
"mage/validation"
"mage/validation/validation"
], factory);
} else {
factory(jQuery);
Expand Down
5 changes: 1 addition & 4 deletions app/code/Magento/Customer/view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ var config = {
address: 'Magento_Customer/address',
setPassword: 'Magento_Customer/set-password'
}
},
deps: [
'mage/validation/dob-rule'
]
}
};
71 changes: 0 additions & 71 deletions lib/web/mage/validation/dob-rule.js

This file was deleted.

181 changes: 116 additions & 65 deletions lib/web/mage/validation/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,76 +15,127 @@
}
}(function ($) {
"use strict";

/**
* Validation rule for grouped product, with multiple qty fields,
* only one qty needs to have a positive integer
*/
$.validator.addMethod(
"validate-grouped-qty",
function(value, element, params) {
var result = false;
var total = 0;
$(params).find('input[data-validate*="validate-grouped-qty"]').each(function(i, e) {
var val = $(e).val();
if (val && val.length > 0) {
result = true;
var valInt = parseInt(val, 10) || 0;
if (valInt >= 0) {
total += valInt;
} else {
result = false;
return result;
}
}
});
return result && total > 0;
},
'Please specify the quantity of product(s).'
);

$.validator.addMethod(
"validate-one-checkbox-required-by-name",
function(value, element, params) {
var checkedCount = 0;
if (element.type === 'checkbox') {
$('[name="' + element.name + '"]').each(function() {
if ($(this).is(':checked')) {
checkedCount += 1;
return false;
$.each({
'validate-grouped-qty': [
function (value, element, params) {
var result = false;
var total = 0;
$(params).find('input[data-validate*="validate-grouped-qty"]').each(function (i, e) {
var val = $(e).val();
if (val && val.length > 0) {
result = true;
var valInt = parseInt(val, 10) || 0;
if (valInt >= 0) {
total += valInt;
} else {
result = false;
return result;
}
}
});
}
var container = '#' + params;
if (checkedCount > 0) {
$(container).removeClass('validation-failed');
$(container).addClass('validation-passed');
return true;
} else {
$(container).addClass('validation-failed');
$(container).removeClass('validation-passed');
return result && total > 0;
},
'Please specify the quantity of product(s).'
],
'validate-one-checkbox-required-by-name': [
function (value, element, params) {
var checkedCount = 0;
if (element.type === 'checkbox') {
$('[name="' + element.name + '"]').each(function () {
if ($(this).is(':checked')) {
checkedCount += 1;
return false;
}
});
}
var container = '#' + params;
if (checkedCount > 0) {
$(container).removeClass('validation-failed');
$(container).addClass('validation-passed');
return true;
} else {
$(container).addClass('validation-failed');
$(container).removeClass('validation-passed');
return false;
}
},
'Please select one of the options.'
],
'validate-date-between': [
function (value, element, params) {
var minDate = new Date(params[0]),
maxDate = new Date(params[1]),
inputDate = new Date(element.value);
minDate.setHours(0);
maxDate.setHours(0);
if (inputDate >= minDate && inputDate <= maxDate) {
return true;
}
this.dateBetweenErrorMessage = $.mage.__('Please enter a date between %min and %max.').replace('%min', minDate).replace('%max', maxDate);
return false;
},
function () {
return this.dateBetweenErrorMessage;
}
},
'Please select one of the options.'
);
],
'validate-dob': [
function (val, element, params) {
var dob = $(element).parents('.customer-dob');
$(dob).find('.' + this.settings.errorClass).removeClass(this.settings.errorClass);
var dayVal = $(dob).find(params[0]).find('input:text').val(),
monthVal = $(dob).find(params[1]).find('input:text').val(),
yearVal = $(dob).find(params[2]).find('input:text').val(),
dobLength = dayVal.length + monthVal.length + yearVal.length;
if (params[3] && dobLength === 0) {
this.dobErrorMessage = 'This is a required field.';
return false;
}
if (!params[3] && dobLength === 0) {
return true;
}
var day = parseInt(dayVal, 10) || 0,
month = parseInt(monthVal, 10) || 0,
year = parseInt(yearVal, 10) || 0,
curYear = (new Date()).getFullYear();
if (!day || !month || !year) {
this.dobErrorMessage = 'Please enter a valid full date.';
return false;
}
if (month < 1 || month > 12) {
this.dobErrorMessage = 'Please enter a valid month (1-12).';
return false;
}
if (year < 1900 || year > curYear) {
this.dobErrorMessage =
$.mage.__('Please enter a valid year (1900-%1).').replace('%1', curYear.toString());
return false;
}
var validateDayInMonth = new Date(year, month, 0).getDate();
if (day < 1 || day > validateDayInMonth) {
this.dobErrorMessage =
$.mage.__('Please enter a valid day (1-%1).').replace('%1', validateDayInMonth.toString());
return false;
}
var today = new Date(),
dateEntered = new Date();
dateEntered.setFullYear(year, month - 1, day);
if (dateEntered > today) {
this.dobErrorMessage = $.mage.__('Please enter a date in the past.');
return false;
}

$.validator.addMethod(
"validate-date-between",
function(value, element, params) {
var minDate = new Date(params[0]),
maxDate = new Date(params[1]),
inputDate = new Date(element.value);
minDate.setHours(0);
maxDate.setHours(0);
if (inputDate >= minDate && inputDate <= maxDate) {
day = day % 10 === day ? '0' + day : day;
month = month % 10 === month ? '0' + month : month;
$(element).val(month + '/' + day + '/' + year);
return true;
},
function () {
return this.dobErrorMessage;
}
this.dateBetweenErrorMessage = $.mage.__('Please enter a date between %min and %max.').replace('%min', minDate).replace('%max', maxDate);
return false;
},
function(){
return this.dateBetweenErrorMessage;
}
);
}));
]
}, function (i, rule) {
rule.unshift(i);
$.validator.addMethod.apply($.validator, rule);
});
}));

0 comments on commit 01cb488

Please sign in to comment.