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.

InlineValidator.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * ~~~
  14. * function foo($attribute, $params)
  15. * ~~~
  16. *
  17. * where `$attribute` refers to the name of the attribute being validated, while `$params`
  18. * is an array representing the additional parameters supplied in the validation rule.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class InlineValidator extends Validator
  24. {
  25. /**
  26. * @var string|\Closure an anonymous function or the name of a model class method that will be
  27. * called to perform the actual validation. The signature of the method should be like the following,
  28. * where `$attribute` is the name of the attribute to be validated, and `$params` contains the value
  29. * of [[params]] that you specify when declaring the inline validation rule:
  30. *
  31. * ~~~
  32. * function foo($attribute, $params)
  33. * ~~~
  34. */
  35. public $method;
  36. /**
  37. * @var mixed additional parameters that are passed to the validation method
  38. */
  39. public $params;
  40. /**
  41. * @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code.
  42. * The signature of the method should be like the following:
  43. *
  44. * ~~~
  45. * function foo($attribute, $params)
  46. * {
  47. * return "javascript";
  48. * }
  49. * ~~~
  50. *
  51. * where `$attribute` refers to the attribute name to be validated.
  52. *
  53. * Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
  54. */
  55. public $clientValidate;
  56. /**
  57. * @inheritdoc
  58. */
  59. public function validateAttribute($model, $attribute)
  60. {
  61. $method = $this->method;
  62. if (is_string($method)) {
  63. $method = [$model, $method];
  64. }
  65. call_user_func($method, $attribute, $this->params);
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function clientValidateAttribute($model, $attribute, $view)
  71. {
  72. if ($this->clientValidate !== null) {
  73. $method = $this->clientValidate;
  74. if (is_string($method)) {
  75. $method = [$model, $method];
  76. }
  77. return call_user_func($method, $attribute, $this->params);
  78. } else {
  79. return null;
  80. }
  81. }
  82. }