InlineValidator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. /**
  9. * InlineValidator represents a validator which is defined as a method in the object being validated.
  10. *
  11. * The validation method must have the following signature:
  12. *
  13. * ```php
  14. * function foo($attribute, $params, $validator)
  15. * ```
  16. *
  17. * where `$attribute` refers to the name of the attribute being validated, while `$params` is an array representing the
  18. * additional parameters supplied in the validation rule. Parameter `$validator` refers to the related
  19. * [[InlineValidator]] object and is available since version 2.0.11.
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @since 2.0
  23. */
  24. class InlineValidator extends Validator
  25. {
  26. /**
  27. * @var string|\Closure an anonymous function or the name of a model class method that will be
  28. * called to perform the actual validation. The signature of the method should be like the following:
  29. *
  30. * ```php
  31. * function foo($attribute, $params, $validator)
  32. * ```
  33. *
  34. * - `$attribute` is the name of the attribute to be validated;
  35. * - `$params` contains the value of [[params]] that you specify when declaring the inline validation rule;
  36. * - `$validator` is a reference to related [[InlineValidator]] object. This parameter is available since version 2.0.11.
  37. */
  38. public $method;
  39. /**
  40. * @var mixed additional parameters that are passed to the validation method
  41. */
  42. public $params;
  43. /**
  44. * @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code.
  45. * The signature of the method should be like the following:
  46. *
  47. * ```php
  48. * function foo($attribute, $params, $validator)
  49. * {
  50. * return "javascript";
  51. * }
  52. * ```
  53. *
  54. * where `$attribute` refers to the attribute name to be validated.
  55. *
  56. * Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
  57. */
  58. public $clientValidate;
  59. /**
  60. * @var mixed the value of attribute being currently validated.
  61. * @since 2.0.36
  62. */
  63. public $current;
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function validateAttribute($model, $attribute)
  68. {
  69. $method = $this->method;
  70. if (is_string($method)) {
  71. $method = [$model, $method];
  72. } elseif ($method instanceof \Closure) {
  73. $method = $this->method->bindTo($model);
  74. }
  75. $current = $this->current;
  76. if ($current === null) {
  77. $current = $model->$attribute;
  78. }
  79. $method($attribute, $this->params, $this, $current);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function clientValidateAttribute($model, $attribute, $view)
  85. {
  86. if ($this->clientValidate !== null) {
  87. $method = $this->clientValidate;
  88. if (is_string($method)) {
  89. $method = [$model, $method];
  90. } elseif ($method instanceof \Closure) {
  91. $method = $method->bindTo($model);
  92. }
  93. $current = $this->current;
  94. if ($current === null) {
  95. $current = $model->$attribute;
  96. }
  97. return $method($attribute, $this->params, $this, $current);
  98. }
  99. return null;
  100. }
  101. }