Support multiple input type

This commit is contained in:
Nicolas Le Goff
2013-06-11 12:28:56 +02:00
parent 7dad897ab7
commit d04a36d23b

View File

@@ -37,8 +37,10 @@ define([
// inputs present in form // inputs present in form
this.inputs = {}; this.inputs = {};
_.each(inputs, function(field) { _.each(_.groupBy(inputs, function(input){
self.inputs[field.name] = field; return input.name;
}), function(fields, name) {
self.inputs[name] = fields;
}); });
this._validateForm(); this._validateForm();
@@ -59,7 +61,8 @@ define([
name: field.name, name: field.name,
rules: field.rules, rules: field.rules,
message: field.message || "An error ocurred on input[name=" + field.name + "], you can edit this message by setting a 'message' property in your rule definition object", message: field.message || "An error ocurred on input[name=" + field.name + "], you can edit this message by setting a 'message' property in your rule definition object",
value: null value: null,
type: field.type || "text"
}); });
}; };
@@ -68,7 +71,18 @@ define([
this.errors = []; this.errors = [];
_.each(this.fields, function(field){ _.each(this.fields, function(field){
if (_.has(self.inputs, field.name)) { if (_.has(self.inputs, field.name)) {
field.value = $.trim(self.inputs[field.name].value); // values can be multiple
var values = [];
_.each(self.inputs[field.name], function(field){
return values.push(field.value);
});
field.value = values.join(',');
self._validateField(field);
} else if (field.type === "checkbox" || field.type === "radio" || field.type === "select" || field.type === "multiple") {
field.value = '';
self._validateField(field); self._validateField(field);
} }
}); });
@@ -79,11 +93,6 @@ define([
var ruleRegex = /^(.+?)\[(.+)\]$/; var ruleRegex = /^(.+?)\[(.+)\]$/;
var rules = field.rules.split('|'); var rules = field.rules.split('|');
// If the value is null and not required, we don't need to run through validation, unless the rule is a callback, but then only if the value is not null
if ((field.rules.indexOf('required') === -1 && (!field.value || field.value === '' || typeof field.value === "undefined")) && (field.rules.indexOf('callback_') === -1 || field.value === null)) {
return;
}
// Run through the rules and execute the validation methods as needed // Run through the rules and execute the validation methods as needed
_.every(rules, function(method) { _.every(rules, function(method) {
var param = null; var param = null;
@@ -115,9 +124,7 @@ define([
// If the hook failed, add a message to the errors array // If the hook failed, add a message to the errors array
if (failed) { if (failed) {
self.errors.push({ self.errors.push({
id: field.id,
name: field.name, name: field.name,
type: field.type,
value: field.value, value: field.value,
message: field.message, message: field.message,
rule: method rule: method
@@ -149,10 +156,6 @@ define([
"required": function(field) { "required": function(field) {
var value = field.value; var value = field.value;
if ((field.type === 'checkbox') || (field.type === 'radio')) {
return (field.checked === true);
}
return (value !== null && value !== ''); return (value !== null && value !== '');
}, },
"equal": function(field, defaultName) { "equal": function(field, defaultName) {
@@ -182,6 +185,10 @@ define([
return true; return true;
}, },
"min_length": function(field, length) { "min_length": function(field, length) {
if (field.type === "multiple") {
return _.filter(field.value.split(","), function(value){ return value !== ""; }).length >= parseInt(length, 10)
}
if (!this.Regexp.numericRegex.test(length)) { if (!this.Regexp.numericRegex.test(length)) {
return false; return false;
} }
@@ -189,6 +196,10 @@ define([
return (field.value.length >= parseInt(length, 10)); return (field.value.length >= parseInt(length, 10));
}, },
"max_length": function(field, length) { "max_length": function(field, length) {
if (field.type === "multiple") {
return _.filter(field.value.split(","), function(value){ return value !== ""; }).length <= parseInt(length, 10)
}
if (!this.Regexp.numericRegex.test(length)) { if (!this.Regexp.numericRegex.test(length)) {
return false; return false;
} }
@@ -196,6 +207,10 @@ define([
return (field.value.length <= parseInt(length, 10)); return (field.value.length <= parseInt(length, 10));
}, },
"exact_length": function(field, length) { "exact_length": function(field, length) {
if (field.type === "multiple") {
return _.filter(field.value.split(","), function(value){ return value !== ""; }).length === parseInt(length, 10)
}
if (!this.Regexp.numericRegex.test(length)) { if (!this.Regexp.numericRegex.test(length)) {
return false; return false;
} }