You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

14 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\validators;
14 years ago
/**
14 years ago
* InlineValidator represents a validator which is defined as a method in the object being validated.
*
* The validation method must have the following signature:
*
* ~~~
* function foo($attribute, $params)
* ~~~
*
* where `$attribute` refers to the name of the attribute being validated, while `$params`
* is an array representing the additional parameters supplied in the validation rule.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class InlineValidator extends Validator
14 years ago
{
/**
* @var string|\Closure an anonymous function or the name of a model class method that will be
11 years ago
* called to perform the actual validation. The signature of the method should be like the following:
*
* ~~~
* function foo($attribute, $params)
* ~~~
14 years ago
*/
public $method;
/**
* @var array additional parameters that are passed to the validation method
*/
public $params;
13 years ago
/**
* @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code.
* The signature of the method should be like the following:
13 years ago
*
* ~~~
11 years ago
* function foo($attribute, $params)
13 years ago
* {
* return "javascript";
* }
* ~~~
*
* where `$attribute` refers to the attribute name to be validated.
*
* Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
13 years ago
*/
public $clientValidate;
14 years ago
/**
* @inheritdoc
14 years ago
*/
14 years ago
public function validateAttribute($object, $attribute)
14 years ago
{
$method = $this->method;
if (is_string($method)) {
$method = [$object, $method];
}
call_user_func($method, $attribute, $this->params);
14 years ago
}
13 years ago
/**
* @inheritdoc
13 years ago
*/
public function clientValidateAttribute($object, $attribute, $view)
13 years ago
{
13 years ago
if ($this->clientValidate !== null) {
13 years ago
$method = $this->clientValidate;
if (is_string($method)) {
$method = [$object, $method];
}
11 years ago
return call_user_func($method, $attribute, $this->params);
} else {
return null;
13 years ago
}
}
}