Yii2 framework backup
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.

109 lines
3.4 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\validators;
13 years ago
/**
13 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:
*
* ```php
* function foo($attribute, $params, $validator)
* ```
13 years ago
*
* 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. Parameter `$validator` refers to the related
* [[InlineValidator]] object and is available since version 2.0.11.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class InlineValidator extends Validator
13 years ago
{
/**
* @var string|\Closure an anonymous function or the name of a model class method that will be
* called to perform the actual validation. The signature of the method should be like the following:
*
* ```php
* function foo($attribute, $params, $validator)
* ```
*
* - `$attribute` is the name of the attribute to be validated;
* - `$params` contains the value of [[params]] that you specify when declaring the inline validation rule;
* - `$validator` is a reference to related [[InlineValidator]] object. This parameter is available since version 2.0.11.
*/
public $method;
/**
10 years ago
* @var mixed additional parameters that are passed to the validation method
*/
public $params;
/**
* @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:
*
* ```php
* function foo($attribute, $params, $validator)
* {
* 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.
*/
public $clientValidate;
/**
* @var mixed the value of attribute being currently validated.
* @since 2.0.36
*/
public $current;
13 years ago
/**
7 years ago
* {@inheritdoc}
*/
public function validateAttribute($model, $attribute)
{
$method = $this->method;
if (is_string($method)) {
$method = [$model, $method];
} elseif ($method instanceof \Closure) {
$method = $this->method->bindTo($model);
}
$current = $this->current;
if ($current === null) {
$current = $model->$attribute;
}
$method($attribute, $this->params, $this, $current);
}
13 years ago
/**
7 years ago
* {@inheritdoc}
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->clientValidate !== null) {
$method = $this->clientValidate;
if (is_string($method)) {
$method = [$model, $method];
} elseif ($method instanceof \Closure) {
$method = $method->bindTo($model);
}
$current = $this->current;
if ($current === null) {
$current = $model->$attribute;
}
return $method($attribute, $this->params, $this, $current);
}
return null;
}
}