RequiredValidator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * 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. }
  74. return [$this->message, [
  75. 'requiredValue' => $this->requiredValue,
  76. ]];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function clientValidateAttribute($model, $attribute, $view)
  82. {
  83. ValidationAsset::register($view);
  84. $options = $this->getClientOptions($model, $attribute);
  85. return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getClientOptions($model, $attribute)
  91. {
  92. $options = [];
  93. if ($this->requiredValue !== null) {
  94. $options['message'] = $this->formatMessage($this->message, [
  95. 'requiredValue' => $this->requiredValue,
  96. ]);
  97. $options['requiredValue'] = $this->requiredValue;
  98. } else {
  99. $options['message'] = $this->message;
  100. }
  101. if ($this->strict) {
  102. $options['strict'] = 1;
  103. }
  104. $options['message'] = $this->formatMessage($options['message'], [
  105. 'attribute' => $model->getAttributeLabel($attribute),
  106. ]);
  107. return $options;
  108. }
  109. }