InlineValidator.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * {@inheritdoc}
  61. */
  62. public function validateAttribute($model, $attribute)
  63. {
  64. $method = $this->method;
  65. if (is_string($method)) {
  66. $method = [$model, $method];
  67. }
  68. call_user_func($method, $attribute, $this->params, $this);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function clientValidateAttribute($model, $attribute, $view)
  74. {
  75. if ($this->clientValidate !== null) {
  76. $method = $this->clientValidate;
  77. if (is_string($method)) {
  78. $method = [$model, $method];
  79. }
  80. return call_user_func($method, $attribute, $this->params, $this);
  81. }
  82. return null;
  83. }
  84. }