CInlineValidator.php 2.6 KB

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