diff options
author | wei <> | 2007-03-27 07:44:02 +0000 |
---|---|---|
committer | wei <> | 2007-03-27 07:44:02 +0000 |
commit | cdf3ba1c190393d86460f8c13073dc9784436b3e (patch) | |
tree | 9c0cb581bb909b933a3f00ef3ed8df88e7c97318 /framework/Web | |
parent | 1b9b64637fe08848f610bbd19b80c031cb7dba63 (diff) |
Fixed #430
Diffstat (limited to 'framework/Web')
-rw-r--r-- | framework/Web/Javascripts/js/compressed/validator.js | 52 | ||||
-rw-r--r-- | framework/Web/Javascripts/js/debug/validator.js | 131 | ||||
-rw-r--r-- | framework/Web/Javascripts/prado/validator/validation3.js | 131 |
3 files changed, 141 insertions, 173 deletions
diff --git a/framework/Web/Javascripts/js/compressed/validator.js b/framework/Web/Javascripts/js/compressed/validator.js index a3b3f1cd..49316113 100644 --- a/framework/Web/Javascripts/js/compressed/validator.js +++ b/framework/Web/Javascripts/js/compressed/validator.js @@ -1,12 +1,14 @@ Prado.Validation=Class.create();Object.extend(Prado.Validation,{managers:{},validate:function(formID,groupID,invoker) -{if(this.managers[formID]) +{formID=formID||this.getForm();if(this.managers[formID]) {return this.managers[formID].validate(groupID,invoker);} else {throw new Error("Form '"+form+"' is not registered with Prado.Validation");}},getForm:function() {var keys=$H(this.managers).keys();return keys[0];},isValid:function(formID,groupID) -{if(this.managers[formID]) -return this.managers[formID].isValid(groupID);return true;},addValidator:function(formID,validator) +{formID=formID||this.getForm();if(this.managers[formID]) +return this.managers[formID].isValid(groupID);return true;},reset:function(groupID) +{var formID=this.getForm();if(this.managers[formID]) +this.managers[formID].reset(groupID);},addValidator:function(formID,validator) {if(this.managers[formID]) this.managers[formID].addValidator(validator);else throw new Error("A validation manager for form '"+formID+"' needs to be created first.");return this.managers[formID];},addSummary:function(formID,validator) @@ -17,36 +19,23 @@ throw new Error("A validation manager for form '"+formID+"' needs to be created {manager[1].validators.each(function(validator) {if(validator.options.ID==validatorID) {validator.options.ErrorMessage=message;$(validatorID).innerHTML=message;}});});}});Prado.ValidationManager=Class.create();Prado.ValidationManager.prototype={initialize:function(options) -{this.validators=[];this.summaries=[];this.groups=[];this.options={};this.options=options;Prado.Validation.managers[options.FormID]=this;},validate:function(group,invoker) -{if(group) -return this._validateGroup(group,invoker);else -return this._validateNonGroup(invoker);},_validateGroup:function(groupID,invoker) -{var valid=true;if(this.groups.include(groupID)) -{this.validators.each(function(validator) -{if(validator.group==groupID) -valid=valid&validator.validate(invoker);else -validator.hide();});} -this.updateSummary(groupID,true);return valid;},_validateNonGroup:function(invoker) -{var valid=true;this.validators.each(function(validator) -{if(!validator.group) -valid=valid&validator.validate(invoker);else -validator.hide();});this.updateSummary(null,true);return valid;},isValid:function(group) -{if(group) -return this._isValidGroup(group);else -return this._isValidNonGroup();},_isValidNonGroup:function() -{var valid=true;this.validators.each(function(validator) -{if(!validator.group) -valid=valid&validator.isValid;});return valid;},_isValidGroup:function(groupID) -{var valid=true;if(this.groups.include(groupID)) -{this.validators.each(function(validator) -{if(validator.group==groupID) -valid=valid&validator.isValid;});} -return valid;},addValidator:function(validator) +{this.validators=[];this.summaries=[];this.groups=[];this.options={};this.options=options;Prado.Validation.managers[options.FormID]=this;},reset:function(group) +{this.validatorPartition(group)[0].invoke('reset');this.updateSummary(group,true);},validate:function(group,source) +{var partition=this.validatorPartition(group);var valid=partition[0].invoke('validate',source).all();partition[1].invoke('hide');this.updateSummary(group,true);return valid;},validatorPartition:function(group) +{return group?this.validatorsInGroup(group):this.validatorsWithoutGroup();},validatorsInGroup:function(groupID) +{if(this.groups.include(groupID)) +{return this.validators.partition(function(val) +{return val.group==groupID;});} +else +return[[],[]];},validatorsWithoutGroup:function() +{return this.validators.partition(function(val) +{return!val.group;});},isValid:function(group) +{return this.validatorPartition(group)[0].pluck('isValid').all();},addValidator:function(validator) {this.validators.push(validator);if(validator.group&&!this.groups.include(validator.group)) this.groups.push(validator.group);},addSummary:function(summary) {this.summaries.push(summary);},getValidatorsWithError:function(group) -{var validators=this.validators.findAll(function(validator) -{var notValid=!validator.isValid;var inGroup=group&&validator.group==group;var noGroup=validator.group==null;return notValid&&(inGroup||noGroup);});return validators;},updateSummary:function(group,refresh) +{return this.validatorPartition(group)[0].findAll(function(validator) +{return!validator.isValid;});},updateSummary:function(group,refresh) {var validators=this.getValidatorsWithError(group);this.summaries.each(function(summary) {var inGroup=group&&summary.group==group;var noGroup=!group&&!summary.group;if(inGroup||noGroup) summary.updateSummary(validators,refresh);else @@ -101,7 +90,8 @@ this.updateControlCssClass(this.control,this.isValid);},updateControlCssClass:fu {if(valid) control.removeClassName(CssClass);else control.addClassName(CssClass);}},hide:function() -{this.isValid=true;this.updateControl();this.visible=false;},validate:function(invoker) +{this.reset();this.visible=false;},reset:function() +{this.isValid=true;this.updateControl();},validate:function(invoker) {if(!this.control) this.control=$(this.options.ControlToValidate);if(!this.control) {this.isValid=true;return this.isValid;} diff --git a/framework/Web/Javascripts/js/debug/validator.js b/framework/Web/Javascripts/js/debug/validator.js index 83453052..179963f4 100644 --- a/framework/Web/Javascripts/js/debug/validator.js +++ b/framework/Web/Javascripts/js/debug/validator.js @@ -78,6 +78,7 @@ Object.extend(Prado.Validation, */
validate : function(formID, groupID, invoker)
{
+ formID = formID || this.getForm();
if(this.managers[formID])
{
return this.managers[formID].validate(groupID, invoker);
@@ -106,12 +107,23 @@ Object.extend(Prado.Validation, */
isValid : function(formID, groupID)
{
+ formID = formID || this.getForm();
if(this.managers[formID])
return this.managers[formID].isValid(groupID);
return true;
},
/**
+ * Reset the validators for a given group.
+ */
+ reset : function(groupID)
+ {
+ var formID = this.getForm();
+ if(this.managers[formID])
+ this.managers[formID].reset(groupID);
+ },
+
+ /**
* Add a new validator to a particular form.
* @param string the form that the validator belongs.
* @param object a validator
@@ -184,102 +196,75 @@ Prado.ValidationManager.prototype = },
/**
- * Validate the validators managed by this validation manager.
- * @param string only validate validators belonging to a group (optional)
- * @param HTMLElement element that calls for validation
- * @return boolean true if all validators are valid, false otherwise.
+ * Reset all validators in the given group (if group is null, validators without a group are used).
*/
- validate : function(group, invoker)
+ reset : function(group)
{
- if(group)
- return this._validateGroup(group, invoker);
- else
- return this._validateNonGroup(invoker);
+ this.validatorPartition(group)[0].invoke('reset');
+ this.updateSummary(group, true);
},
/**
- * Validate a particular group of validators.
- * @param string ID of the form
+ * Validate the validators managed by this validation manager.
+ * @param string only validate validators belonging to a group (optional)
* @param HTMLElement element that calls for validation
- * @return boolean false if group is not valid, true otherwise.
+ * @return boolean true if all validators are valid, false otherwise.
*/
- _validateGroup: function(groupID, invoker)
+ validate : function(group, source)
{
- var valid = true;
- if(this.groups.include(groupID))
- {
- this.validators.each(function(validator)
- {
- if(validator.group == groupID)
- valid = valid & validator.validate(invoker);
- else
- validator.hide();
- });
- }
- this.updateSummary(groupID, true);
+ var partition = this.validatorPartition(group);
+ var valid = partition[0].invoke('validate', source).all();
+ partition[1].invoke('hide');
+ this.updateSummary(group, true);
return valid;
},
+
/**
- * Validate validators that doesn't belong to any group.
- * @return boolean false if not valid, true otherwise.
- * @param HTMLElement element that calls for validation
+ * @return array[0] validators belong to a group if group is given, otherwise validators
+ * not belongining to any group. array[1] the opposite of array[0].
*/
- _validateNonGroup : function(invoker)
+ validatorPartition : function(group)
{
- var valid = true;
- this.validators.each(function(validator)
- {
- if(!validator.group)
- valid = valid & validator.validate(invoker);
- else
- validator.hide();
- });
- this.updateSummary(null, true);
- return valid;
+ return group ? this.validatorsInGroup(group) : this.validatorsWithoutGroup();
},
/**
- * Gets the state of all the validators, true if they are all valid.
- * @return boolean true if the validators are valid.
+ * @return array validatiors in a given group in first array and
+ * validators not belonging to the group in 2nd array.
*/
- isValid : function(group)
+ validatorsInGroup : function(groupID)
{
- if(group)
- return this._isValidGroup(group);
+ if(this.groups.include(groupID))
+ {
+ return this.validators.partition(function(val)
+ {
+ return val.group == groupID;
+ });
+ }
else
- return this._isValidNonGroup();
+ return [[],[]];
},
/**
- * @return boolean true if all validators not belonging to a group are valid.
+ * @return array validators without any group in first array, and those
+ * with groups in 2nd array.
*/
- _isValidNonGroup : function()
+ validatorsWithoutGroup : function()
{
- var valid = true;
- this.validators.each(function(validator)
+ return this.validators.partition(function(val)
{
- if(!validator.group)
- valid = valid & validator.isValid;
+ return !val.group;
});
- return valid;
},
/**
- * @return boolean true if all validators belonging to the group are valid.
+ * Gets the state of all the validators, true if they are all valid.
+ * @return boolean true if the validators are valid.
*/
- _isValidGroup : function(groupID)
+ isValid : function(group)
{
- var valid = true;
- if(this.groups.include(groupID))
- {
- this.validators.each(function(validator)
- {
- if(validator.group == groupID)
- valid = valid & validator.isValid;
- });
- }
- return valid;
+ return this.validatorPartition(group)[0].pluck('isValid').all();
},
/**
@@ -309,14 +294,10 @@ Prado.ValidationManager.prototype = */
getValidatorsWithError : function(group)
{
- var validators = this.validators.findAll(function(validator)
+ return this.validatorPartition(group)[0].findAll(function(validator)
{
- var notValid = !validator.isValid;
- var inGroup = group && validator.group == group;
- var noGroup = validator.group == null;
- return notValid && (inGroup || noGroup);
+ return !validator.isValid;
});
- return validators;
},
/**
@@ -662,9 +643,17 @@ Prado.WebUI.TBaseValidator.prototype = */
hide : function()
{
+ this.reset();
+ this.visible = false;
+ },
+
+ /**
+ * Sets isValid = true and updates the validator display.
+ */
+ reset : function()
+ {
this.isValid = true;
this.updateControl();
- this.visible = false;
},
/**
diff --git a/framework/Web/Javascripts/prado/validator/validation3.js b/framework/Web/Javascripts/prado/validator/validation3.js index a1062c38..9b5b4046 100644 --- a/framework/Web/Javascripts/prado/validator/validation3.js +++ b/framework/Web/Javascripts/prado/validator/validation3.js @@ -78,6 +78,7 @@ Object.extend(Prado.Validation, */
validate : function(formID, groupID, invoker)
{
+ formID = formID || this.getForm();
if(this.managers[formID])
{
return this.managers[formID].validate(groupID, invoker);
@@ -106,12 +107,23 @@ Object.extend(Prado.Validation, */
isValid : function(formID, groupID)
{
+ formID = formID || this.getForm();
if(this.managers[formID])
return this.managers[formID].isValid(groupID);
return true;
},
/**
+ * Reset the validators for a given group.
+ */
+ reset : function(groupID)
+ {
+ var formID = this.getForm();
+ if(this.managers[formID])
+ this.managers[formID].reset(groupID);
+ },
+
+ /**
* Add a new validator to a particular form.
* @param string the form that the validator belongs.
* @param object a validator
@@ -184,102 +196,75 @@ Prado.ValidationManager.prototype = },
/**
- * Validate the validators managed by this validation manager.
- * @param string only validate validators belonging to a group (optional)
- * @param HTMLElement element that calls for validation
- * @return boolean true if all validators are valid, false otherwise.
+ * Reset all validators in the given group (if group is null, validators without a group are used).
*/
- validate : function(group, invoker)
+ reset : function(group)
{
- if(group)
- return this._validateGroup(group, invoker);
- else
- return this._validateNonGroup(invoker);
+ this.validatorPartition(group)[0].invoke('reset');
+ this.updateSummary(group, true);
},
/**
- * Validate a particular group of validators.
- * @param string ID of the form
+ * Validate the validators managed by this validation manager.
+ * @param string only validate validators belonging to a group (optional)
* @param HTMLElement element that calls for validation
- * @return boolean false if group is not valid, true otherwise.
+ * @return boolean true if all validators are valid, false otherwise.
*/
- _validateGroup: function(groupID, invoker)
+ validate : function(group, source)
{
- var valid = true;
- if(this.groups.include(groupID))
- {
- this.validators.each(function(validator)
- {
- if(validator.group == groupID)
- valid = valid & validator.validate(invoker);
- else
- validator.hide();
- });
- }
- this.updateSummary(groupID, true);
+ var partition = this.validatorPartition(group);
+ var valid = partition[0].invoke('validate', source).all();
+ partition[1].invoke('hide');
+ this.updateSummary(group, true);
return valid;
},
+
/**
- * Validate validators that doesn't belong to any group.
- * @return boolean false if not valid, true otherwise.
- * @param HTMLElement element that calls for validation
+ * @return array[0] validators belong to a group if group is given, otherwise validators
+ * not belongining to any group. array[1] the opposite of array[0].
*/
- _validateNonGroup : function(invoker)
+ validatorPartition : function(group)
{
- var valid = true;
- this.validators.each(function(validator)
- {
- if(!validator.group)
- valid = valid & validator.validate(invoker);
- else
- validator.hide();
- });
- this.updateSummary(null, true);
- return valid;
+ return group ? this.validatorsInGroup(group) : this.validatorsWithoutGroup();
},
/**
- * Gets the state of all the validators, true if they are all valid.
- * @return boolean true if the validators are valid.
+ * @return array validatiors in a given group in first array and
+ * validators not belonging to the group in 2nd array.
*/
- isValid : function(group)
+ validatorsInGroup : function(groupID)
{
- if(group)
- return this._isValidGroup(group);
+ if(this.groups.include(groupID))
+ {
+ return this.validators.partition(function(val)
+ {
+ return val.group == groupID;
+ });
+ }
else
- return this._isValidNonGroup();
+ return [[],[]];
},
/**
- * @return boolean true if all validators not belonging to a group are valid.
+ * @return array validators without any group in first array, and those
+ * with groups in 2nd array.
*/
- _isValidNonGroup : function()
+ validatorsWithoutGroup : function()
{
- var valid = true;
- this.validators.each(function(validator)
+ return this.validators.partition(function(val)
{
- if(!validator.group)
- valid = valid & validator.isValid;
+ return !val.group;
});
- return valid;
},
/**
- * @return boolean true if all validators belonging to the group are valid.
+ * Gets the state of all the validators, true if they are all valid.
+ * @return boolean true if the validators are valid.
*/
- _isValidGroup : function(groupID)
+ isValid : function(group)
{
- var valid = true;
- if(this.groups.include(groupID))
- {
- this.validators.each(function(validator)
- {
- if(validator.group == groupID)
- valid = valid & validator.isValid;
- });
- }
- return valid;
+ return this.validatorPartition(group)[0].pluck('isValid').all();
},
/**
@@ -309,14 +294,10 @@ Prado.ValidationManager.prototype = */
getValidatorsWithError : function(group)
{
- var validators = this.validators.findAll(function(validator)
+ return this.validatorPartition(group)[0].findAll(function(validator)
{
- var notValid = !validator.isValid;
- var inGroup = group && validator.group == group;
- var noGroup = validator.group == null;
- return notValid && (inGroup || noGroup);
+ return !validator.isValid;
});
- return validators;
},
/**
@@ -662,9 +643,17 @@ Prado.WebUI.TBaseValidator.prototype = */
hide : function()
{
+ this.reset();
+ this.visible = false;
+ },
+
+ /**
+ * Sets isValid = true and updates the validator display.
+ */
+ reset : function()
+ {
this.isValid = true;
this.updateControl();
- this.visible = false;
},
/**
|