TBaseValidator serves as the base class for validator 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. You can also manually perform validation by calling {@link TPage::validate()}. The input control to be validated is specified by {@link setControlToValidate ControlToValidate}. Validator controls always validate the associated input control on the serve side. In addition, if {@link getEnableClientScript 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. You can use multiple validator controls to validate a single input control, each responsible for validating against a different criteria. For example, on a user registration form, you may want to make sure the user enters a value in the username text box, and the input must consist of only word characters. You can use a {@link TRequiredFieldValidator} to ensure the input of username and a {@link TRegularExpressionValidator} to ensure the proper input. If an input control fails validation, the text specified by the {@link setErrorMessage ErrorMessage} property is displayed in the validation control. However, if the {@link setText Text} property is set, it will be displayed instead. If both {@link setErrorMessage ErrorMessage} and {@link setText Text} are empty, the body content of the validator will be displayed. Error display is controlled by {@link setDisplay Display} property. You can also customized the client-side behaviour by adding javascript code to the subproperties of the {@link getClientSide ClientSide} property. See quickstart documentation for further details. You can also place a {@link TValidationSummary} control on a page to display error messages from the validators together. In this case, only the {@link setErrorMessage ErrorMessage} property of the validators will be displayed in the {@link TValidationSummary} control. Validators can be partitioned into validation groups by setting their {@link setValidationGroup ValidationGroup} property. If the control causing the validation also sets its ValidationGroup property, only those validators having the same ValidationGroup value will do input validation. Note, the {@link TPage::getIsValid IsValid} property of the current {@link TPage} instance will be automatically updated by the validation process which occurs after {@link TPage::onLoad onLoad} of {@link TPage} and before the postback events. Therefore, if you use the {@link TPage::getIsValid()} property in the {@link TPage::onLoad()} method, you must first explicitly call the {@link TPage::validate()} method. Notes to Inheritors When you inherit from TBaseValidator, you must override the method {@link evaluateIsValid}.
Since: 3.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends TLabel, implements Prado\Web\UI\IValidator
Example #1
0
 public function onPreRender($param)
 {
     parent::onPreRender($param);
     $cs = $this->Page->getClientScript();
     $cs->registerPradoScript('validator');
     // communicate validation status to the client side
     $value = $this->_isvalid === false ? '0' : '1';
     $cs->registerHiddenField($this->getClientID() . '_1', $value);
     // update validator display
     if ($control = $this->getValidationTarget()) {
         $fn = 'captchaUpdateValidatorStatus_' . $this->getClientID();
         // check if we need to request a new captcha too
         if ($this->Page->IsCallback) {
             if ($control->getVisible(true)) {
                 if (!is_null($this->_isvalid)) {
                     // if the response has been tested and we reach the pre-render phase
                     // then we need to regenerate the token, because it won't test positive
                     // anymore, even if solves correctly
                     $control->regenerateToken();
                 }
             }
         }
         $cs->registerEndScript($this->getClientID() . '::validate', implode(' ', array('function ' . $fn . '(valid)', '{', '  jQuery(' . TJavaScript::quoteString('#' . $this->getClientID() . '_1') . ').val(valid);', '  Prado.Validation.validateControl(' . TJavaScript::quoteString($control->ClientID) . '); ', '}', '', $this->Page->IsCallback ? $fn . '(' . $value . ');' : '', '', 'jQuery("#' . $control->getClientID() . '").on("keyup", ' . TJavaScript::quoteString('#' . $control->getResponseFieldName()) . ', function() { ', $fn . '("1");', '});')));
     }
 }
Example #2
0
 public function onPreRender($param)
 {
     parent::onPreRender($param);
     $cs = $this->Page->getClientScript();
     $cs->registerPradoScript('validator');
     // communicate validation status to the client side
     $value = $this->_isvalid === false ? '0' : '1';
     $cs->registerHiddenField($this->getClientID() . '_1', $value);
     // update validator display
     if ($control = $this->getValidationTarget()) {
         $fn = 'captchaUpdateValidatorStatus_' . $this->getClientID();
         $cs->registerEndScript($this->getClientID() . '::validate', implode(' ', array('function ' . $fn . '(valid)', '{', '  jQuery(' . TJavaScript::quoteString('#' . $this->getClientID() . '_1') . ').val(valid);', '  Prado.Validation.validateControl(' . TJavaScript::quoteString($control->ClientID) . '); ', '}', '', $this->Page->IsCallback ? $fn . '(' . $value . ');' : '', '', 'jQuery("#' . $control->getClientID() . '").on("change", ' . TJavaScript::quoteString('#' . $control->getResponseFieldName()) . ', function() { ', $fn . '("1");', '});')));
     }
 }
Example #3
0
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     if (($name = $this->getControlToCompare()) !== '') {
         if (($control = $this->findControl($name)) !== null) {
             $options['ControlToCompare'] = $control->getClientID();
         }
     }
     if (($value = $this->getValueToCompare()) !== '') {
         $options['ValueToCompare'] = $value;
     }
     if (($operator = $this->getOperator()) !== TValidationCompareOperator::Equal) {
         $options['Operator'] = $operator;
     }
     $options['DataType'] = $this->getDataType();
     if (($dateFormat = $this->getDateFormat()) !== '') {
         $options['DateFormat'] = $dateFormat;
     }
     return $options;
 }
 /**
  * Ensure that the ID attribute is rendered and registers the javascript code
  * for initializing the active control.
  */
 protected function addAttributesToRender($writer)
 {
     parent::addAttributesToRender($writer);
     TBaseValidator::registerClientScriptValidator();
 }
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     $control = $this->getValidationTarget();
     if (!$control instanceof TListControl) {
         throw new TConfigurationException('listcontrolvalidator_invalid_control', $this->getID(), $this->getControlToValidate(), get_class($control));
     }
     $min = $this->getMinSelection();
     $max = $this->getMaxSelection();
     if ($min !== -1) {
         $options['Min'] = $min;
     }
     if ($max !== -1) {
         $options['Max'] = $max;
     }
     $required = $this->getRequiredSelections();
     if (strlen($required) > 0) {
         $options['Required'] = $required;
     }
     $options['TotalItems'] = $control->getItemCount();
     return $options;
 }
Example #6
0
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     $control = $this->findCaptchaControl();
     if ($control->getCaseSensitive()) {
         $options['TokenHash'] = $this->generateTokenHash($control->getToken());
         $options['CaseSensitive'] = true;
     } else {
         $options['TokenHash'] = $this->generateTokenHash(strtoupper($control->getToken()));
         $options['CaseSensitive'] = false;
     }
     return $options;
 }
Example #7
0
 /**
  * Only register the client-side validator if
  * {@link setClientValidationFunction ClientValidationFunction} is set.
  */
 protected function registerClientScriptValidator()
 {
     if ($this->getClientValidationFunction() !== '') {
         parent::registerClientScriptValidator();
     }
 }
Example #8
0
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     $options['DataType'] = $this->getDataType();
     if (($dateFormat = $this->getDateFormat()) !== '') {
         $options['DateFormat'] = $dateFormat;
     }
     return $options;
 }
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     $options['InitialValue'] = $this->getInitialValue();
     $control = $this->getValidationTarget();
     if ($control instanceof TListControl) {
         $options['TotalItems'] = $control->getItemCount();
     }
     if ($control instanceof TRadioButton && strlen($control->getGroupName()) > 0) {
         $options['GroupName'] = $control->getGroupName();
     }
     return $options;
 }
Example #10
0
 /**
  * Returns an array of javascript validator options.
  * @return array javascript validator options.
  */
 protected function getClientScriptOptions()
 {
     $options = parent::getClientScriptOptions();
     $options['MinValue'] = $this->getMinValue();
     $options['MaxValue'] = $this->getMaxValue();
     $options['DataType'] = $this->getDataType();
     $options['StrictComparison'] = $this->getStrictComparison();
     if (($dateFormat = $this->getDateFormat()) !== '') {
         $options['DateFormat'] = $dateFormat;
     }
     return $options;
 }