1: <?php
2: /**
3: * CInlineValidator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CInlineValidator represents a validator which is defined as a method in the object being validated.
13: *
14: * @author Qiang Xue <qiang.xue@gmail.com>
15: * @package system.validators
16: * @since 1.0
17: */
18: class CInlineValidator extends CValidator
19: {
20: /**
21: * @var string the name of the validation method defined in the active record class
22: */
23: public $method;
24: /**
25: * @var array additional parameters that are passed to the validation method
26: */
27: public $params;
28: /**
29: * @var string the name of the method that returns the client validation code (See {@link clientValidateAttribute}).
30: */
31: public $clientValidate;
32:
33: /**
34: * Validates the attribute of the object.
35: * If there is any error, the error message is added to the object.
36: * @param CModel $object the object being validated
37: * @param string $attribute the attribute being validated
38: */
39: protected function validateAttribute($object,$attribute)
40: {
41: $method=$this->method;
42: $object->$method($attribute,$this->params);
43: }
44:
45: /**
46: * Returns the JavaScript code needed to perform client-side validation by calling the {@link clientValidate} method.
47: * In the client validation code, these variables are predefined:
48: * <ul>
49: * <li>value: the current input value associated with this attribute.</li>
50: * <li>messages: an array that may be appended with new error messages for the attribute.</li>
51: * <li>attribute: a data structure keeping all client-side options for the attribute</li>
52: * </ul>
53: * <b>Example</b>:
54: *
55: * If {@link clientValidate} is set to "clientValidate123", clientValidate123() is the name of
56: * the method that returns the client validation code and can look like:
57: * <pre>
58: * <?php
59: * public function clientValidate123($attribute,$params)
60: * {
61: * if(!isset($params['message']))
62: * $params['message']='Value should be 123';
63: * $js = "if(value != '123') { messages.push($params['message']); }";
64: * return $js;
65: * }
66: * ?>
67: * </pre>
68: * @param CModel $object the data object being validated
69: * @param string $attribute the name of the attribute to be validated.
70: * @return string the client-side validation script.
71: * @see CActiveForm::enableClientValidation
72: * @since 1.1.9
73: */
74: public function clientValidateAttribute($object,$attribute)
75: {
76: if($this->clientValidate!==null)
77: {
78: $method=$this->clientValidate;
79: return $object->$method($attribute,$this->params);
80: }
81: }
82: }
83: