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.

RequiredValidator.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. use Yii;
  9. /**
  10. * RequiredValidator validates that the specified attribute does not have null or empty value.
  11. *
  12. * @author Qiang Xue <qiang.xue@gmail.com>
  13. * @since 2.0
  14. */
  15. class RequiredValidator extends Validator
  16. {
  17. /**
  18. * @var boolean whether to skip this validator if the value being validated is empty.
  19. */
  20. public $skipOnEmpty = false;
  21. /**
  22. * @var mixed the desired value that the attribute must have.
  23. * If this is null, the validator will validate that the specified attribute is not empty.
  24. * If this is set as a value that is not null, the validator will validate that
  25. * the attribute has a value that is the same as this property value.
  26. * Defaults to null.
  27. * @see strict
  28. */
  29. public $requiredValue;
  30. /**
  31. * @var boolean whether the comparison between the attribute value and [[requiredValue]] is strict.
  32. * When this is true, both the values and types must match.
  33. * Defaults to false, meaning only the values need to match.
  34. * Note that when [[requiredValue]] is null, if this property is true, the validator will check
  35. * if the attribute value is null; If this property is false, the validator will call [[isEmpty]]
  36. * to check if the attribute value is empty.
  37. */
  38. public $strict = false;
  39. /**
  40. * @var string the user-defined error message. It may contain the following placeholders which
  41. * will be replaced accordingly by the validator:
  42. *
  43. * - `{attribute}`: the label of the attribute being validated
  44. * - `{value}`: the value of the attribute being validated
  45. * - `{requiredValue}`: the value of [[requiredValue]]
  46. */
  47. public $message;
  48. /**
  49. * @inheritdoc
  50. */
  51. public function init()
  52. {
  53. parent::init();
  54. if ($this->message === null) {
  55. $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
  56. : Yii::t('yii', '{attribute} must be "{requiredValue}".');
  57. }
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. protected function validateValue($value)
  63. {
  64. if ($this->requiredValue === null) {
  65. if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty(is_string($value) ? trim($value) : $value)) {
  66. return null;
  67. }
  68. } elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
  69. return null;
  70. }
  71. if ($this->requiredValue === null) {
  72. return [$this->message, []];
  73. } else {
  74. return [$this->message, [
  75. 'requiredValue' => $this->requiredValue,
  76. ]];
  77. }
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function clientValidateAttribute($model, $attribute, $view)
  83. {
  84. $options = [];
  85. if ($this->requiredValue !== null) {
  86. $options['message'] = Yii::$app->getI18n()->format($this->message, [
  87. 'requiredValue' => $this->requiredValue,
  88. ], Yii::$app->language);
  89. $options['requiredValue'] = $this->requiredValue;
  90. } else {
  91. $options['message'] = $this->message;
  92. }
  93. if ($this->strict) {
  94. $options['strict'] = 1;
  95. }
  96. $options['message'] = Yii::$app->getI18n()->format($options['message'], [
  97. 'attribute' => $model->getAttributeLabel($attribute),
  98. ], Yii::$app->language);
  99. ValidationAsset::register($view);
  100. return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  101. }
  102. }