123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace yii\validators;
- use Yii;
- class StringValidator extends Validator
- {
-
- public $length;
-
- public $max;
-
- public $min;
-
- public $message;
-
- public $tooShort;
-
- public $tooLong;
-
- public $notEqual;
-
- public $encoding;
-
- public function init()
- {
- parent::init();
- if (is_array($this->length)) {
- if (isset($this->length[0])) {
- $this->min = $this->length[0];
- }
- if (isset($this->length[1])) {
- $this->max = $this->length[1];
- }
- $this->length = null;
- }
- if ($this->encoding === null) {
- $this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
- }
- if ($this->message === null) {
- $this->message = Yii::t('yii', '{attribute} must be a string.');
- }
- if ($this->min !== null && $this->tooShort === null) {
- $this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.');
- }
- if ($this->max !== null && $this->tooLong === null) {
- $this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.');
- }
- if ($this->length !== null && $this->notEqual === null) {
- $this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.');
- }
- }
-
- public function validateAttribute($model, $attribute)
- {
- $value = $model->$attribute;
- if (!is_string($value)) {
- $this->addError($model, $attribute, $this->message);
- return;
- }
- $length = mb_strlen($value, $this->encoding);
- if ($this->min !== null && $length < $this->min) {
- $this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
- }
- if ($this->max !== null && $length > $this->max) {
- $this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
- }
- if ($this->length !== null && $length !== $this->length) {
- $this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
- }
- }
-
- protected function validateValue($value)
- {
- if (!is_string($value)) {
- return [$this->message, []];
- }
- $length = mb_strlen($value, $this->encoding);
- if ($this->min !== null && $length < $this->min) {
- return [$this->tooShort, ['min' => $this->min]];
- }
- if ($this->max !== null && $length > $this->max) {
- return [$this->tooLong, ['max' => $this->max]];
- }
- if ($this->length !== null && $length !== $this->length) {
- return [$this->notEqual, ['length' => $this->length]];
- }
- return null;
- }
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- ValidationAsset::register($view);
- $options = $this->getClientOptions($model, $attribute);
- return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
- }
-
- public function getClientOptions($model, $attribute)
- {
- $label = $model->getAttributeLabel($attribute);
- $options = [
- 'message' => $this->formatMessage($this->message, [
- 'attribute' => $label,
- ]),
- ];
- if ($this->min !== null) {
- $options['min'] = $this->min;
- $options['tooShort'] = $this->formatMessage($this->tooShort, [
- 'attribute' => $label,
- 'min' => $this->min,
- ]);
- }
- if ($this->max !== null) {
- $options['max'] = $this->max;
- $options['tooLong'] = $this->formatMessage($this->tooLong, [
- 'attribute' => $label,
- 'max' => $this->max,
- ]);
- }
- if ($this->length !== null) {
- $options['is'] = $this->length;
- $options['notEqual'] = $this->formatMessage($this->notEqual, [
- 'attribute' => $label,
- 'length' => $this->length,
- ]);
- }
- if ($this->skipOnEmpty) {
- $options['skipOnEmpty'] = 1;
- }
- return $options;
- }
- }
|