From c26c9396b0a307ed7a37c7ca6be7b695ab538fb3 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 28 Dec 2011 17:32:44 -0500 Subject: [PATCH] ... --- framework/validators/InlineValidator.php | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/framework/validators/InlineValidator.php b/framework/validators/InlineValidator.php index dd5d5be..e942d49 100644 --- a/framework/validators/InlineValidator.php +++ b/framework/validators/InlineValidator.php @@ -34,6 +34,20 @@ class InlineValidator extends Validator * @var array additional parameters that are passed to the validation method */ public $params; + /** + * @var string the name of the method that returns the client validation code (see [[clientValidateAttribute()]] + * for details on how to return client validation code). The signature of the method should be like the following: + * + * ~~~ + * function foo($attribute) + * { + * return "javascript"; + * } + * ~~~ + * + * where `$attribute` refers to the attribute name to be validated. + */ + public $clientValidate; /** * Validates the attribute of the object. @@ -45,4 +59,31 @@ class InlineValidator extends Validator $method = $this->method; $object->$method($attribute, $this->params); } + + /** + * Returns the JavaScript needed for performing client-side validation. + * + * You may override this method to return the JavaScript validation code if + * the validator can support client-side validation. + * + * The following JavaScript variables are predefined and can be used in the validation code: + * + * - `attribute`: the name of the attribute being validated. + * - `value`: the value being validated. + * - `messages`: an array used to hold the validation error messages for the attribute. + * + * @param \yii\base\Model $object the data object being validated + * @param string $attribute the name of the attribute to be validated. + * @return string the client-side validation script. Null if the validator does not support + * client-side validation. + * @see enableClientValidation + * @see \yii\web\ActiveForm::enableClientValidation + */ + public function clientValidateAttribute($object, $attribute) + { + if($this->clientValidate !== null) { + $method = $this->clientValidate; + return $object->$method($attribute); + } + } }