RequiredValidator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 bool 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 bool 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. *
  35. * Note that behavior for when [[requiredValue]] is null is the following:
  36. *
  37. * - In strict mode, the validator will check if the attribute value is null
  38. * - In non-strict mode validation will fail
  39. */
  40. public $strict = false;
  41. /**
  42. * @var string the user-defined error message. It may contain the following placeholders which
  43. * will be replaced accordingly by the validator:
  44. *
  45. * - `{attribute}`: the label of the attribute being validated
  46. * - `{value}`: the value of the attribute being validated
  47. * - `{requiredValue}`: the value of [[requiredValue]]
  48. */
  49. public $message;
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function init()
  54. {
  55. parent::init();
  56. if ($this->message === null) {
  57. $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
  58. : Yii::t('yii', '{attribute} must be "{requiredValue}".');
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function validateValue($value)
  65. {
  66. if ($this->requiredValue === null) {
  67. if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty(is_string($value) ? trim($value) : $value)) {
  68. return null;
  69. }
  70. } elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
  71. return null;
  72. }
  73. if ($this->requiredValue === null) {
  74. return [$this->message, []];
  75. }
  76. return [$this->message, [
  77. 'requiredValue' => $this->requiredValue,
  78. ]];
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function clientValidateAttribute($model, $attribute, $view)
  84. {
  85. ValidationAsset::register($view);
  86. $options = $this->getClientOptions($model, $attribute);
  87. return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getClientOptions($model, $attribute)
  93. {
  94. $options = [];
  95. if ($this->requiredValue !== null) {
  96. $options['message'] = $this->formatMessage($this->message, [
  97. 'requiredValue' => $this->requiredValue,
  98. ]);
  99. $options['requiredValue'] = $this->requiredValue;
  100. } else {
  101. $options['message'] = $this->message;
  102. }
  103. if ($this->strict) {
  104. $options['strict'] = 1;
  105. }
  106. $options['message'] = $this->formatMessage($options['message'], [
  107. 'attribute' => $model->getAttributeLabel($attribute),
  108. ]);
  109. return $options;
  110. }
  111. }