Commit c26c9396 by Qiang Xue

...

parent 702476d8
...@@ -34,6 +34,20 @@ class InlineValidator extends Validator ...@@ -34,6 +34,20 @@ class InlineValidator extends Validator
* @var array additional parameters that are passed to the validation method * @var array additional parameters that are passed to the validation method
*/ */
public $params; 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. * Validates the attribute of the object.
...@@ -45,4 +59,31 @@ class InlineValidator extends Validator ...@@ -45,4 +59,31 @@ class InlineValidator extends Validator
$method = $this->method; $method = $this->method;
$object->$method($attribute, $this->params); $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);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment