|
- <?php
-
-
- namespace yii\validators;
-
- use Yii;
-
-
- class RequiredValidator extends Validator
- {
-
-
- public $skipOnEmpty = false;
-
-
- public $requiredValue;
-
-
- public $strict = false;
-
-
- public $message;
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->message === null) {
- $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
- : Yii::t('yii', '{attribute} must be "{requiredValue}".');
- }
- }
-
-
-
- protected function validateValue($value)
- {
- if ($this->requiredValue === null) {
- if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty(is_string($value) ? trim($value) : $value)) {
- return null;
- }
- } elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
- return null;
- }
- if ($this->requiredValue === null) {
- return [$this->message, []];
- } else {
- return [$this->message, [
- 'requiredValue' => $this->requiredValue,
- ]];
- }
- }
-
-
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- $options = [];
- if ($this->requiredValue !== null) {
- $options['message'] = Yii::$app->getI18n()->format($this->message, [
- 'requiredValue' => $this->requiredValue,
- ], Yii::$app->language);
- $options['requiredValue'] = $this->requiredValue;
- } else {
- $options['message'] = $this->message;
- }
- if ($this->strict) {
- $options['strict'] = 1;
- }
-
- $options['message'] = Yii::$app->getI18n()->format($options['message'], [
- 'attribute' => $model->getAttributeLabel($attribute),
- ], Yii::$app->language);
-
- ValidationAsset::register($view);
-
- return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
- }
- }
|