|
|
@ -21,17 +21,24 @@ use yii\helpers\Html; |
|
|
|
class StringValidator extends Validator |
|
|
|
class StringValidator extends Validator |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var integer maximum length. Defaults to null, meaning no maximum limit. |
|
|
|
* @var integer|array specifies the length limit of the value to be validated. |
|
|
|
|
|
|
|
* This can be specified in one of the following forms: |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* - an integer: the exact length that the value should be of; |
|
|
|
|
|
|
|
* - an array of one element: the minimum length that the value should be of. For example, `array(8)`. |
|
|
|
|
|
|
|
* This will overwrite [[min]]. |
|
|
|
|
|
|
|
* - an array of two elements: the minimum and maximum lengths that the value should be of. |
|
|
|
|
|
|
|
* For example, `array(8, 128)`. This will overwrite both [[min]] and [[max]]. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $max; |
|
|
|
public $length; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var integer minimum length. Defaults to null, meaning no minimum limit. |
|
|
|
* @var integer maximum length. If not set, it means no maximum length limit. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $min; |
|
|
|
public $max; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var integer exact length. Defaults to null, meaning no exact length limit. |
|
|
|
* @var integer minimum length. If not set, it means no minimum length limit. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $is; |
|
|
|
public $min; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string user-defined error message used when the value is not a string |
|
|
|
* @var string user-defined error message used when the value is not a string |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -61,6 +68,15 @@ class StringValidator extends Validator |
|
|
|
public function init() |
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
parent::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) { |
|
|
|
if ($this->encoding === null) { |
|
|
|
$this->encoding = Yii::$app->charset; |
|
|
|
$this->encoding = Yii::$app->charset; |
|
|
|
} |
|
|
|
} |
|
|
@ -73,7 +89,7 @@ class StringValidator extends Validator |
|
|
|
if ($this->max !== null && $this->tooLong === null) { |
|
|
|
if ($this->max !== null && $this->tooLong === null) { |
|
|
|
$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max} characters.'); |
|
|
|
$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max} characters.'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->is !== null && $this->notEqual === null) { |
|
|
|
if ($this->length !== null && $this->notEqual === null) { |
|
|
|
$this->notEqual = Yii::t('yii', '{attribute} should contain {length} characters.'); |
|
|
|
$this->notEqual = Yii::t('yii', '{attribute} should contain {length} characters.'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -101,8 +117,8 @@ class StringValidator extends Validator |
|
|
|
if ($this->max !== null && $length > $this->max) { |
|
|
|
if ($this->max !== null && $length > $this->max) { |
|
|
|
$this->addError($object, $attribute, $this->tooLong, array('{max}' => $this->max)); |
|
|
|
$this->addError($object, $attribute, $this->tooLong, array('{max}' => $this->max)); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->is !== null && $length !== $this->is) { |
|
|
|
if ($this->length !== null && $length !== $this->length) { |
|
|
|
$this->addError($object, $attribute, $this->notEqual, array('{length}' => $this->is)); |
|
|
|
$this->addError($object, $attribute, $this->notEqual, array('{length}' => $this->length)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -119,7 +135,7 @@ class StringValidator extends Validator |
|
|
|
$length = mb_strlen($value, $this->encoding); |
|
|
|
$length = mb_strlen($value, $this->encoding); |
|
|
|
return ($this->min === null || $length >= $this->min) |
|
|
|
return ($this->min === null || $length >= $this->min) |
|
|
|
&& ($this->max === null || $length <= $this->max) |
|
|
|
&& ($this->max === null || $length <= $this->max) |
|
|
|
&& ($this->is === null || $length === $this->is); |
|
|
|
&& ($this->length === null || $length === $this->length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -158,8 +174,8 @@ class StringValidator extends Validator |
|
|
|
'{max}' => $this->max, |
|
|
|
'{max}' => $this->max, |
|
|
|
))); |
|
|
|
))); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->is !== null) { |
|
|
|
if ($this->length !== null) { |
|
|
|
$options['is'] = $this->is; |
|
|
|
$options['is'] = $this->length; |
|
|
|
$options['notEqual'] = Html::encode(strtr($this->notEqual, array( |
|
|
|
$options['notEqual'] = Html::encode(strtr($this->notEqual, array( |
|
|
|
'{attribute}' => $label, |
|
|
|
'{attribute}' => $label, |
|
|
|
'{value}' => $value, |
|
|
|
'{value}' => $value, |
|
|
|