From 8569373e8cb6163f182fe13ffbc44ea1b2c961cd Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 23 Feb 2006 03:38:15 +0000 Subject: more clean up work about validators. --- .gitattributes | 2 + .../Controls/Samples/TCompareValidator/Home.page | 34 +++- .../Controls/Samples/TDataTypeValidator/Home.page | 57 +++++++ .../Controls/Samples/TDataTypeValidator/Home.php | 8 + .../protected/pages/Controls/Validation.page | 178 ++++++++------------- framework/Web/UI/WebControls/TCompareValidator.php | 22 +-- .../Web/UI/WebControls/TDataTypeValidator.php | 24 ++- framework/Web/UI/WebControls/TRangeValidator.php | 14 +- 8 files changed, 199 insertions(+), 140 deletions(-) create mode 100644 demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.page create mode 100644 demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.php diff --git a/.gitattributes b/.gitattributes index 0d8b8ab2..9358e4bd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -146,6 +146,8 @@ demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample1.page -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample1.php -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php -text +demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.page -text +demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.php -text demos/quickstart/protected/pages/Controls/Samples/TDropDownList/Home.page -text demos/quickstart/protected/pages/Controls/Samples/TDropDownList/Home.php -text demos/quickstart/protected/pages/Controls/Samples/TEmailAddressValidator/Home.page -text diff --git a/demos/quickstart/protected/pages/Controls/Samples/TCompareValidator/Home.page b/demos/quickstart/protected/pages/Controls/Samples/TCompareValidator/Home.page index ab99436a..c5cec8af 100644 --- a/demos/quickstart/protected/pages/Controls/Samples/TCompareValidator/Home.page +++ b/demos/quickstart/protected/pages/Controls/Samples/TCompareValidator/Home.page @@ -1,7 +1,7 @@

TCompareValidator Samples

-

Note:TCompareValidator will start +

Note: TCompareValidator will start to validate only if both inputs are not empty.

@@ -39,6 +39,38 @@ Compare validator with client-side validation disabled: + + + + + + + + + +
+Comparing with a constant value: + + + + +
+Comparing with an integer: + + + + +
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.page b/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.page new file mode 100644 index 00000000..783769ff --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.page @@ -0,0 +1,57 @@ + + +

TDataTypeValidator Samples

+

Note: TDataTypeValidator will start +to validate only if both inputs are not empty.

+ + + + + + + + + + + + + + + + + + +
+Data type validator with default settings: + + + + +
+Data type validator with client-side validation disabled: + + + + +
+Validating a date input: + + + + +
+ +
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.php b/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.php new file mode 100644 index 00000000..88d2dbf2 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/TDataTypeValidator/Home.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Validation.page b/demos/quickstart/protected/pages/Controls/Validation.page index 766925ca..6fe87fd0 100644 --- a/demos/quickstart/protected/pages/Controls/Validation.page +++ b/demos/quickstart/protected/pages/Controls/Validation.page @@ -2,137 +2,74 @@

Validation Controls

-

Validation is performed when a postback control, such as a TButton, a TLinkButton or a TTextBox (under AutoPostBack mode) is submitting the page and its CausesValidation property is true.

- -

Validator controls always validate the associated input control on the serve side. -In addition, if EnableClientScript is true, validation will also be performed on the client-side using javascript. Client-side validation will validate user input before it is sent to the server. The form data will not be submitted if any error is detected. This avoids the round-trip of information necessary for server-side validation.

- -

Every validator component has the following properties, defined in the TBaseValidator class. - -

-
ControlToValidate
-
The ID of the component for this validator. This property must be set to the ID path of an input component. The ID path is the dot-connected IDs of the components reaching from the validator's parent component to the target component.
- -
ErrorMessage
-
The text for the error message when the input component failed to validate.
- -
ValidationGroup
-
If the control causing the validation also sets its ValidationGroup property, only those validators having the same ValidationGroup value will do input validation. -
- -
Display
-
The display behavior of the error message in a validation component. The allowed values are: None, Static and Dynamic. The default is Static. - -
+

+Validation controls, called validators, perform validation on user-entered data values when they are post back to the server. The validation is triggered by a postback control, such as a TButton, a TLinkButton or a TTextBox (under AutoPostBack mode) whose CausesValidation property is true. +

-
EnableClientScript
-
Indicating whether client-side validation is enabled. Default is true.
-
+

+Validation is always performed on server side. If EnableClientScript is true and the client browser supports JavaScript, validators may also perform client-side validation. Client-side validation will validate user input before it is sent to the server. The form data will not be submitted if any error is detected. This avoids the round-trip of information necessary for server-side validation. +

+

+Validators share a common set of properties, which are defined in the base class TBaseValidator class and listed as follows, +

+

TRequiredFieldValidator

-This is the simplest validator, ensuring that the input field has some sort of value. To ensure that all of our input fields are required, add a TRequiredFieldValidator component for each of the input fields. The TRequiredFieldValidator also has the following property. +TRequiredFieldValidator ensures that the user enters some data in the specified input field. By default, TRequiredFieldValidator will check if the user input is empty or not. The validation fails if the input is empty. By setting InitialValue, the validator can check if the user input is different from InitialValue. If not, the validation fails.

-
-
InitialValue
-
The associated input component fails validation if its value does not change from the InitialValue upon losing focus.
-
-

TRegularExpressionValidator

-

The TRegularExpressionValidator has the following property in addition to the parent TBaseValidator properties. -

-
-
RegularExpression
-
The regular expression that determines the pattern used to validate a field. Some commonly used regular expressions include: -
    -
  • At least 6 characters: [\w]{6,}
  • -
  • Japanese Phone Number: (0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}
  • -
  • Japanese Postal Code: \d{3}(-(\d{4}|\d{2}))?
  • -
  • P.R.C. Phone Number: (\(\d{3}\)|\d{3}-)?\d{8}
  • -
  • P.R.C. Postal Code: \d{6}
  • -
  • P.R.C. Social Security Number: \d{18}|\d{15}
  • -
  • U.S. Phone Number: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
  • -
  • U.S. ZIP Code: \d{5}(-\d{4})?
  • -
  • U.S. Social Security Number: \d{3}-\d{2}-\d{4}
  • -
- More regular expression patterns can be found on the Internet, e.g. - http://regexlib.com/. -
-
-

-Note, if the value being validated is empty, TRegularExpressionValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty. +

+TRegularExpressionValidator verifies the user input against a regular pattern. The validation fails if the input does not match the pattern. The regular expression can be specified by the RegularExpression property. Some commonly used regular expressions include: +

+ +

+More regular expression patterns can be found on the Internet, e.g. +http://regexlib.com/. +

+

+Note, TRegularExpressionValidator only checks for nonempty user input. Use a TRequiredFieldValidator to ensure the user input is not empty.

-

TEmailAddressValidator

-

TEmailAddressValidator validates whether the value of an associated input component is a valid email address. If CheckMXRecord is true, besides checking the format of the input value, the validator will also check MX record for the email address, provided checkdnsrr() is available in the installed PHP. +

+TEmailAddressValidator verifies that the user input is a valid email address. The validator uses a regular expression to check if the input is in a valid email address format. If CheckMXRecord is true, the validator will also check whether the MX record indicated by the email address is valid, provided checkdnsrr() is available in the installed PHP.

-Note, if the value being validated is empty, TEmailAddressValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty. +Note, if the input being validated is empty, TEmailAddressValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty.

TCompareValidator

-

The validator TCompareValidator is used to compare two input fields, the comparison can be made in many ways. The following are the properties of the TCompareValidator in addition to the parent TBaseValidator. -

-
-
ControlToCompare
-
The input component to compare with the input control being validated.
- -
ValueToCompare
-
A constant value to compare with the value entered by the user into the input component being validated.
- -
ValueType
-
The data type (Integer, Double, Currency, Date, String) that the values being compared are converted to before the comparison is made.
- -
Operator
-
The comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck).
- -
DateFormat
-
The date format to use during comparision.
-
- -

To specify the input component to validate, set the ControlToValidate property to the ID of the input component. To compare the associated input component with another input component, set the ControlToCompare property to the ID of the component to compare with.

- -

To compare the associated input component with a constant value, specify the constant value to compare with by setting the ValueToCompare property.

- -

The ValueType property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following value types are supported.

-
-
Integer
-
A 32-bit signed integer data type.
- -
Float
-
A double-precision floating point number data type.
- -
Currency
-
A decimal data type that can contain currency symbols.
- -
Date
-
A date data type, the date format depends on the DateFormat property.
- -
String
-
A string data type.
-
- -

Use the Operator property to specify the type of comparison to perform. If you set the Operator property to DataTypeCheck, the TCompareValidator component will ignore the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered into the input component can be converted to the data type specified by the ValueType property.

- -

Note that if the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data into the input control.

- - -

TRangeValidator

-

-TRangeValidator tests whether an input value is within a specified range. -

-TRangeValidator uses three key properties to perform its validation. The MinValue and MaxValue properties specify the minimum and maximum values of the valid range. The ValueType property specifies the data type of the value being validated. The value will be first converted into the specified type and then compare with the valid range. The following value types are supported: +TCompareValidator compares the user input with a constant value specified by ValueToCompare, or another user input specified by ControlToCompare. The Operator property specifies how to compare the values, which includes Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan and LessThanEqual. Before comparison, the values being compared will be converted to the type specified by DataType listed as follows,

  • String - A string data type.
  • @@ -141,13 +78,23 @@ TRangeValidator uses three key properties to perform its validation. The Min
  • Currency - A decimal data type that can contain currency symbols.
  • Date - A date data type. The date format can be specified by setting DateFormat property, which must be recognizable by TSimpleDateFormatter. If the property is not set, the GNU date syntax is assumed.
- +

+Note, if the input being validated is empty, TEmailAddressValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty. +

+

TDataTypeValidator

+TDataTypeValidator verifies if the input data is of specific type indicated by DataType. The data types that can be checked against are the same as those in TCompareValidator.

+

TRangeValidator

+

+TRangeValidator verifies whether an input value is within a specified range. TRangeValidator uses three key properties to perform its validation. The MinValue and MaxValue properties specify the minimum and maximum values of the valid range. The DataType property specifies the data type of the value being validated. The value will be first converted into the specified type and then compare with the valid range. The data types that can be checked against are the same as those in TCompareValidator. +

+ +

TCustomValidator

TCustomValidator performs user-defined validation (either server-side or client-side or both) on an input control. @@ -174,6 +121,13 @@ function ValidationFunctionName(sender, parameter)

TValidationSummary

+TValidationSummary displays a summary of validation errors inline on a Web page, in a message box, or both. +

+

+By default, a validation summary will collect ErrorMessage of all failed validators on the page. If ValidationGroup is not empty, only those validators who belong to the group will show their error messages in the summary. +

+

+The summary can be displayed as a list, a bulleted list, or a single paragraph based on the DisplayMode property. The messages shown can be prefixed with HeaderText. The summary can be displayed on the Web page or in a JavaScript message box, by setting the ShowSummary and ShowMessageBox properties, respectively. Note, the latter is only effective when EnableClientScript is true.

diff --git a/framework/Web/UI/WebControls/TCompareValidator.php b/framework/Web/UI/WebControls/TCompareValidator.php index 6cc9d0e5..811ca50a 100644 --- a/framework/Web/UI/WebControls/TCompareValidator.php +++ b/framework/Web/UI/WebControls/TCompareValidator.php @@ -26,7 +26,7 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); * a constant value, specify the constant value to compare with by setting the * {@link setValueToCompare ValueToCompare} property. * - * The {@link setValueType ValueType} property is used to specify the data type + * The {@link setDataType DataType} property is used to specify the data type * of both comparison values. Both values are automatically converted to this data * type before the comparison operation is performed. The following value types are supported: * - Integer A 32-bit signed integer data type. @@ -37,13 +37,7 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); * * Use the {@link setOperator Operator} property to specify the type of comparison * to perform. Valid operators include Equal, NotEqual, GreaterThan, GreaterThanEqual, - * LessThan, LessThanEqual, and DataTypeCheck. - * - * Note, if you set {@link setOperator Operator} to DataTypeCheck, the validator - * will ignore the {@link setControlToCompare ControlToCompare} and - * {@link setValueToCompare ValueToCompare} properties and simply - * indicates whether the value entered into the input control can be converted - * to the data type specified by the {@link setValueType ValueType} property. + * LessThan and LessThanEqual. * * @author Qiang Xue * @version $Revision: $ $Date: $ @@ -55,18 +49,18 @@ class TCompareValidator extends TBaseValidator /** * @return string the data type that the values being compared are converted to before the comparison is made. Defaults to String. */ - public function getValueType() + public function getDataType() { - return $this->getViewState('ValueType','String'); + return $this->getViewState('DataType','String'); } /** * Sets the data type (Integer, Float, Currency, Date, String) that the values being compared are converted to before the comparison is made. * @param string the data type */ - public function setValueType($value) + public function setDataType($value) { - $this->setViewState('ValueType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); + $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); } /** @@ -187,7 +181,7 @@ class TCompareValidator extends TBaseValidator */ protected function getComparisonValues($value1, $value2) { - switch($this->getValueType()) + switch($this->getDataType()) { case 'Integer': return array(intval($value1), intval($value2)); @@ -232,7 +226,7 @@ class TCompareValidator extends TBaseValidator $options['valuetocompare']=$value; if(($operator=$this->getOperator())!=='Equal') $options['operator']=$operator; - $options['type']=$this->getValueType(); + $options['type']=$this->getDataType(); if(($dateFormat=$this->getDateFormat())!=='') $options['dateformat']=$dateFormat; return $options; diff --git a/framework/Web/UI/WebControls/TDataTypeValidator.php b/framework/Web/UI/WebControls/TDataTypeValidator.php index daaa9b6e..f3cf5a3c 100644 --- a/framework/Web/UI/WebControls/TDataTypeValidator.php +++ b/framework/Web/UI/WebControls/TDataTypeValidator.php @@ -18,6 +18,18 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); /** * TDataTypeValidator class * + * TDataTypeValidator verifies if the input data is of the type specified + * by {@link setDataType DataType}. + * The following data types are supported: + * - Integer A 32-bit signed integer data type. + * - Float A double-precision floating point number data type. + * - Currency A decimal data type that can contain currency symbols. + * - Date A date data type. + * - String A string data type. + * For Date type, the property {@link setDateFormat DateFormat} + * will be used to determine how to parse the date string. If it is not + * provided, the string will be assumed to be in GNU datetime format. + * * @author Wei Zhuo * @version $Revision: $ $Date: $ * @package System.Web.UI.WebControls @@ -28,18 +40,18 @@ class TDataTypeValidator extends TBaseValidator /** * @return string the data type that the values being compared are converted to before the comparison is made. Defaults to String. */ - public function getValueType() + public function getDataType() { - return $this->getViewState('ValueType','String'); + return $this->getViewState('DataType','String'); } /** - * Sets the data type (Integer, Double, Currency, Date, String) that the values being compared are converted to before the comparison is made. + * Sets the data type (Integer, Float, Currency, Date, String) that the values being compared are converted to before the comparison is made. * @param string the data type */ - public function setValueType($value) + public function setDataType($value) { - $this->setViewState('ValueType',TPropertyValue::ensureEnum($value,'Integer','Double','Date','Currency','String'),'String'); + $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); } /** @@ -67,7 +79,7 @@ class TDataTypeValidator extends TBaseValidator */ protected function evaluateDataTypeCheck($value) { - switch($this->getValueType()) + switch($this->getDataType()) { case 'Integer': return preg_match('/^[-+]?[0-9]+$/',trim($value)); diff --git a/framework/Web/UI/WebControls/TRangeValidator.php b/framework/Web/UI/WebControls/TRangeValidator.php index e66221df..a861c9d6 100644 --- a/framework/Web/UI/WebControls/TRangeValidator.php +++ b/framework/Web/UI/WebControls/TRangeValidator.php @@ -23,7 +23,7 @@ Prado::using('System.Web.UI.WebControls.TBaseValidator'); * TRangeValidator uses three key properties to perform its validation. * The {@link setMinValue MinValue} and {@link setMaxValue MaxValue} * properties specify the minimum and maximum values of the valid range. - * The {@link setValueType ValueType} property is used to specify the + * The {@link setDataType DataType} property is used to specify the * data type of the value and the minimum and maximum range values. * These values are converted to this data type before the validation * operation is performed. The following value types are supported: @@ -81,9 +81,9 @@ class TRangeValidator extends TBaseValidator * @return string the data type that the values being compared are * converted to before the comparison is made. Defaults to String. */ - public function getValueType() + public function getDataType() { - return $this->getViewState('ValueType','String'); + return $this->getViewState('DataType','String'); } /** @@ -91,9 +91,9 @@ class TRangeValidator extends TBaseValidator * being compared are converted to before the comparison is made. * @param string the data type */ - public function setValueType($value) + public function setDataType($value) { - $this->setViewState('ValueType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); + $this->setViewState('DataType',TPropertyValue::ensureEnum($value,'Integer','Float','Date','Currency','String'),'String'); } /** @@ -125,7 +125,7 @@ class TRangeValidator extends TBaseValidator if($value==='') return true; - switch($this->getValueType()) + switch($this->getDataType()) { case 'Integer': return $this->isValidInteger($value); @@ -273,7 +273,7 @@ class TRangeValidator extends TBaseValidator $options=parent::getClientScriptOptions(); $options['minimumvalue']=$this->getMinValue(); $options['maximumvalue']=$this->getMaxValue(); - $options['type']=$this->getValueType(); + $options['type']=$this->getDataType(); if(($dateFormat=$this->getDateFormat())!=='') $options['dateformat']=$dateFormat; return $options; -- cgit v1.2.3