From cdf3ba1c190393d86460f8c13073dc9784436b3e Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 27 Mar 2007 07:44:02 +0000 Subject: Fixed #430 --- HISTORY | 1 + .../protected/pages/Controls/Validation.page | 8 ++ .../Web/Javascripts/js/compressed/validator.js | 52 ++++---- framework/Web/Javascripts/js/debug/validator.js | 131 ++++++++++----------- .../Web/Javascripts/prado/validator/validation3.js | 131 ++++++++++----------- tests/FunctionalTests/index.php | 2 +- 6 files changed, 151 insertions(+), 174 deletions(-) diff --git a/HISTORY b/HISTORY index e4ee9543..c098cf21 100644 --- a/HISTORY +++ b/HISTORY @@ -13,6 +13,7 @@ BUG: SelectedIndices not return expected result for ActiveListBox (Wei) ENH: Ticket#503 - Localization and parameter tags can now appear as a substring in a property initial value (Qiang) ENH: Ticket#513 - Display last modified / revision in quickstart (Wei) ENH: Ticket#519 - Update TActiveRecord implementation (Wei) +ENH: Ticket#430 - clearing the validator errors (Wei) ENH: Added PRADO_CHMOD constant so that users can specify the permission of PRADO-created directories (Qiang) ENH: Added Display property to TWebControl (Wei) ENH: Added TUser.getState() and setState() for storing user session data (Qiang) diff --git a/demos/quickstart/protected/pages/Controls/Validation.page b/demos/quickstart/protected/pages/Controls/Validation.page index 981ca5c8..38699550 100644 --- a/demos/quickstart/protected/pages/Controls/Validation.page +++ b/demos/quickstart/protected/pages/Controls/Validation.page @@ -31,6 +31,14 @@ Validators share a common set of properties, which are defined in the base class
  • FocusElementID - the ID of the HTML element that will receive focus if validation fails and FocusOnError is true.
  • +

    Interacting the Validators with Javascript

    +

    Resetting or Clearing of Validators

    +

    +Validators can be reset on the client-side using javascript by calling the +Prado.Validation.reset(groupID) where groupID is the validator +grouping name. If groupID is null, then validators without grouping are used. +

    +

    TRequiredFieldValidator

    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,11 +107,22 @@ 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. @@ -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; }, /** @@ -661,10 +642,18 @@ Prado.WebUI.TBaseValidator.prototype = * Hides the validator messages and remove any validation changes. */ 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,11 +107,22 @@ 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. @@ -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; }, /** @@ -661,10 +642,18 @@ Prado.WebUI.TBaseValidator.prototype = * Hides the validator messages and remove any validation changes. */ 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/tests/FunctionalTests/index.php b/tests/FunctionalTests/index.php index 5c4dfcc7..319ca3dc 100644 --- a/tests/FunctionalTests/index.php +++ b/tests/FunctionalTests/index.php @@ -8,7 +8,7 @@ Prado Functional Test Suites

    Prado Functional Test Suites