summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HISTORY1
-rw-r--r--demos/quickstart/protected/pages/Controls/Validation.page8
-rw-r--r--framework/Web/Javascripts/js/compressed/validator.js52
-rw-r--r--framework/Web/Javascripts/js/debug/validator.js131
-rw-r--r--framework/Web/Javascripts/prado/validator/validation3.js131
-rw-r--r--tests/FunctionalTests/index.php2
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
<li><tt>FocusElementID</tt> - the ID of the HTML element that will receive focus if validation fails and <tt>FocusOnError</tt> is true.</li>
</ul>
+<h1 id="123123">Interacting the Validators with Javascript</h1>
+<h2>Resetting or Clearing of Validators</h2>
+<p id="1212323">
+Validators can be reset on the client-side using javascript by calling the
+<tt>Prado.Validation.reset(groupID)</tt> where <tt>groupID</tt> is the validator
+grouping name. If <tt>groupID</tt> is null, then validators without grouping are used.
+</p>
+
<a name="TRequiredFieldValidator"></a>
<h2 id="4902">TRequiredFieldValidator</h2>
<p id="560340" class="block-content">
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;
},
/**
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
<h1>Prado Functional Test Suites</h1>
<ul>
<li><a href="quickstart.php">Tests of QuickStart Tutorial Demo</a></li>
- <li><a href="validators.php">Tests of Validators</a></li>
+ <li><a href="validators.php">Tests of Validators</a> (<a href="validators/index.php">list of test samples</a>)</li>
<li><a href="active.php">Tests of Active Controls</a> (<a href="active-controls/index.php">list of test samples</a>) </li>
<li><a href="tickets.php">Tests of Trac Tickets</a> (<a href="tickets/index.php">list of Test Samples</a>)</li>
<li><a href="features.php">Tests of New Features</a> (<a href="features/index.php">list of new features</a>)</li>