Commit a23f2334 by Qiang Xue

enhanced the usage of StringValidator.

parent 4e1264ef
...@@ -237,8 +237,8 @@ function rules() ...@@ -237,8 +237,8 @@ function rules()
{ {
return array( return array(
// rule applied when corresponding field is "safe" // rule applied when corresponding field is "safe"
array('username', 'string', 'min' => 2), array('username', 'string', 'length' => array(4, 32)),
array('first_name', 'string', 'min' => 2), array('first_name', 'string', 'max' => 128),
array('password', 'required'), array('password', 'required'),
// rule applied when scenario is "signup" no matter if field is "safe" or not // rule applied when scenario is "signup" no matter if field is "safe" or not
......
...@@ -21,17 +21,24 @@ use yii\helpers\Html; ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment