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.
102 lines
3.2 KiB
102 lines
3.2 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @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:
|
||
|
*
|
||
|
* ~~~
|
||
|
* 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.
|
||
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
|
{
|
||
|
/**
|
||
12 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. Note that if you use anonymous function, you cannot
|
||
|
* use `$this` in it unless you are using PHP 5.4 or above.
|
||
13 years ago
|
*/
|
||
|
public $method;
|
||
|
/**
|
||
|
* @var array additional parameters that are passed to the validation method
|
||
|
*/
|
||
|
public $params;
|
||
13 years ago
|
/**
|
||
12 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
|
*
|
||
|
* ~~~
|
||
|
* function foo($attribute)
|
||
|
* {
|
||
|
* return "javascript";
|
||
|
* }
|
||
|
* ~~~
|
||
|
*
|
||
|
* where `$attribute` refers to the attribute name to be validated.
|
||
12 years ago
|
*
|
||
|
* Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
|
||
13 years ago
|
*/
|
||
|
public $clientValidate;
|
||
13 years ago
|
|
||
|
/**
|
||
|
* Validates the attribute of the object.
|
||
13 years ago
|
* @param \yii\base\Model $object the object being validated
|
||
13 years ago
|
* @param string $attribute the attribute being validated
|
||
|
*/
|
||
13 years ago
|
public function validateAttribute($object, $attribute)
|
||
13 years ago
|
{
|
||
|
$method = $this->method;
|
||
12 years ago
|
if (is_string($method)) {
|
||
|
$method = array($object, $method);
|
||
|
}
|
||
|
call_user_func($method, $attribute, $this->params);
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
|
/**
|
||
|
* 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.
|
||
12 years ago
|
* @param \yii\base\View $view the view object that is going to be used to render views or view files
|
||
|
* containing a model form with this validator applied.
|
||
13 years ago
|
* @return string the client-side validation script. Null if the validator does not support
|
||
|
* client-side validation.
|
||
|
* @see enableClientValidation
|
||
|
* @see \yii\web\ActiveForm::enableClientValidation
|
||
|
*/
|
||
12 years ago
|
public function clientValidateAttribute($object, $attribute, $view)
|
||
13 years ago
|
{
|
||
13 years ago
|
if ($this->clientValidate !== null) {
|
||
13 years ago
|
$method = $this->clientValidate;
|
||
12 years ago
|
if (is_string($method)) {
|
||
|
$method = array($object, $method);
|
||
|
}
|
||
|
return call_user_func($method, $attribute);
|
||
12 years ago
|
} else {
|
||
|
return null;
|
||
13 years ago
|
}
|
||
|
}
|
||
12 years ago
|
}
|