From acfdc83a0bb620d996c80a0787fd515121fcdca0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Mon, 16 Jan 2006 05:26:31 +0000 Subject: Added validator controls. --- framework/Web/UI/WebControls/TCustomValidator.php | 161 ++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 framework/Web/UI/WebControls/TCustomValidator.php (limited to 'framework/Web/UI/WebControls/TCustomValidator.php') diff --git a/framework/Web/UI/WebControls/TCustomValidator.php b/framework/Web/UI/WebControls/TCustomValidator.php new file mode 100644 index 00000000..bf80e644 --- /dev/null +++ b/framework/Web/UI/WebControls/TCustomValidator.php @@ -0,0 +1,161 @@ + + * @version $Revision: 1.7 $ $Date: 2005/06/13 07:04:28 $ + * @package System.Web.UI.WebControls + */ + +/** + * TValidator class file + */ +require_once(dirname(__FILE__).'/TValidator.php'); + +/** + * TCustomValidator class + * + * TCustomValidator performs user-defined validation (either + * server-side or client-side or both) on an input component. + * + * To create a server-side validation function, provide a handler for + * the OnServerValidate event that performs the validation. + * The data string of the input component to validate can be accessed + * by the value property of the event parameter which is of type + * TServerValidateEventParameter. The result of the validation + * should be stored in the isValid property of the event parameter. + * + * To create a client-side validation function, add the client-side + * validation javascript function to the page template. + * The function should have the following signature: + * + * + * + * Use the ClientValidationFunction property to specify the name of + * the client-side validation script function associated with the TCustomValidator. + * + * Namespace: System.Web.UI.WebControls + * + * Properties + * - ClientValidationFunction, string, kept in viewstate + *
Gets or sets the name of the custom client-side script function used for validation. + * + * Events + * - OnServerValidate Occurs when validation is performed on the server. + *
Event delegates must set the event parameter TServerValidateEventParameter.isValid + * to false if they find the value is invalid. + * + * @author Qiang Xue + * @version v1.0, last update on 2004/08/13 21:44:52 + * @package System.Web.UI.WebControls + */ +class TCustomValidator extends TValidator +{ + /** + * @return string the name of the custom client-side script function used for validation. + */ + public function getClientValidationFunction() + { + return $this->getViewState('ClientValidationFunction',''); + } + + /** + * Sets the name of the custom client-side script function used for validation. + * @param string the script function name + */ + public function setClientValidationFunction($value) + { + $this->setViewState('ClientValidationFunction',$value,''); + } + + /** + * This method overrides the parent's implementation. + * The validation succeeds if {@link onServerValidate} returns true. + * @return boolean whether the validation succeeds + */ + public function evaluateIsValid() + { + $idPath=$this->getControlToValidate(); + if(strlen($idPath)) + { + $control=$this->getTargetControl($idPath); + $value=$control->getValidationPropertyValue($idPath); + return $this->onServerValidate($value); + } + else + return true; + } + + /** + * This method is invoked when the server side validation happens. + * It will raise the OnServerValidate event. + * The method also allows derived classes to handle the event without attaching a delegate. + * Note The derived classes should call parent implementation + * to ensure the OnServerValidate event is raised. + * @param string the value to be validated + * @return boolean whether the value is valid + */ + public function onServerValidate($value) + { + $param=new TServerValidateEventParameter; + $param->value=$value; + $param->isValid=true; + $this->raiseEvent('OnServerValidate',$this,$param); + return $param->isValid; + } + + /** + * Get a list of options for the client-side javascript validator + * @return array list of options for the validator + */ + protected function getJsOptions() + { + $options = parent::getJsOptions(); + $clientJs = $this->getClientValidationFunction(); + if(strlen($clientJs)) + $options['clientvalidationfunction']=$clientJs; + return $options; + } +} + +/** + * TServerValidateEventParameter class + * + * TServerValidateEventParameter encapsulates the parameter data for + * OnServerValidate event of TCustomValidator components. + * + * @author Qiang Xue + * @version v1.0, last update on 2004/08/13 21:44:52 + * @package System.Web.UI.WebControls + */ +class TServerValidateEventParameter extends TEventParameter +{ + /** + * the value to be validated + * @var string + */ + public $value=''; + /** + * whether the value is valid + * @var boolean + */ + public $isValid=true; +} +?> \ No newline at end of file -- cgit v1.2.3