Finished js validation for string validator.

This commit is contained in:
Qiang Xue
2013-05-02 11:16:31 -04:00
parent 5069c29b98
commit 4e7a33bcf5
2 changed files with 38 additions and 25 deletions

View File

@@ -8,6 +8,7 @@
namespace yii\validators;
use Yii;
use yii\helpers\Html;
/**
* StringValidator validates that the attribute value is of certain length.
@@ -132,56 +133,47 @@ class StringValidator extends Validator
$label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
$message = strtr($this->message, array(
'{attribute}' => $label,
'{value}' => $value,
));
$notEqual = strtr($this->notEqual, array(
'{attribute}' => $label,
'{value}' => $value,
'{length}' => $this->is,
));
$tooShort = strtr($this->tooShort, array(
'{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min,
));
$tooLong = strtr($this->tooLong, array(
'{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max,
));
$js = '';
$options = array(
'message' => Html::encode($message),
'notEqual' => Html::encode($notEqual),
'tooShort' => Html::encode($tooShort),
'tooLong' => Html::encode($tooLong),
);
if ($this->min !== null) {
$js .= "
if(value.length< {$this->min}) {
messages.push(" . json_encode($tooShort) . ");
}
";
$options['min'] = $this->min;
}
if ($this->max !== null) {
$js .= "
if(value.length> {$this->max}) {
messages.push(" . json_encode($tooLong) . ");
}
";
$options['max'] = $this->max;
}
if ($this->is !== null) {
$js .= "
if(value.length!= {$this->is}) {
messages.push(" . json_encode($notEqual) . ");
}
";
$options['is'] = $this->is;
}
if ($this->skipOnEmpty) {
$js = "
if($.trim(value)!='') {
$js
}
";
$options['skipOnEmpty'] = 1;
}
return $js;
return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
}
}