Commit 0c7a75be by Qiang Xue

w

parent ef4ed7c8
...@@ -42,7 +42,7 @@ class CBooleanValidator extends Validator ...@@ -42,7 +42,7 @@ class CBooleanValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -63,7 +63,7 @@ class CBooleanValidator extends Validator ...@@ -63,7 +63,7 @@ class CBooleanValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
...@@ -40,7 +40,7 @@ class CCaptchaValidator extends Validator ...@@ -40,7 +40,7 @@ class CCaptchaValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -82,7 +82,7 @@ class CCaptchaValidator extends Validator ...@@ -82,7 +82,7 @@ class CCaptchaValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
...@@ -69,7 +69,7 @@ class CCompareValidator extends Validator ...@@ -69,7 +69,7 @@ class CCompareValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -138,7 +138,7 @@ class CCompareValidator extends Validator ...@@ -138,7 +138,7 @@ class CCompareValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
...@@ -44,7 +44,7 @@ class CDateValidator extends Validator ...@@ -44,7 +44,7 @@ class CDateValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
...@@ -34,7 +34,7 @@ class CDefaultValueValidator extends Validator ...@@ -34,7 +34,7 @@ class CDefaultValueValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
<?php <?php
/** /**
* CEmailValidator class file. * EmailValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,14 +10,12 @@ ...@@ -10,14 +10,12 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CEmailValidator validates that the attribute value is a valid email address. * EmailValidator validates that the attribute value is a valid email address.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CEmailValidator.php 3242 2011-05-28 14:31:04Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CEmailValidator extends Validator class EmailValidator extends Validator
{ {
/** /**
* @var string the regular expression used to validate the attribute value. * @var string the regular expression used to validate the attribute value.
...@@ -26,14 +24,12 @@ class CEmailValidator extends Validator ...@@ -26,14 +24,12 @@ class CEmailValidator extends Validator
public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/'; public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
/** /**
* @var string the regular expression used to validate email addresses with the name part. * @var string the regular expression used to validate email addresses with the name part.
* This property is used only when {@link allowName} is true. * This property is used only when [[allowName]] is true.
* @since 1.0.5
* @see allowName * @see allowName
*/ */
public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/'; public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';
/** /**
* @var boolean whether to allow name in the email address (e.g. "Qiang Xue <qiang.xue@gmail.com>"). Defaults to false. * @var boolean whether to allow name in the email address (e.g. "Qiang Xue <qiang.xue@gmail.com>"). Defaults to false.
* @since 1.0.5
* @see fullPattern * @see fullPattern
*/ */
public $allowName = false; public $allowName = false;
...@@ -46,7 +42,6 @@ class CEmailValidator extends Validator ...@@ -46,7 +42,6 @@ class CEmailValidator extends Validator
/** /**
* @var boolean whether to check port 25 for the email address. * @var boolean whether to check port 25 for the email address.
* Defaults to false. * Defaults to false.
* @since 1.0.4
*/ */
public $checkPort = false; public $checkPort = false;
/** /**
...@@ -58,16 +53,16 @@ class CEmailValidator extends Validator ...@@ -58,16 +53,16 @@ class CEmailValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
if (!$this->validateValue($value)) }
{ if (!$this->validateValue($value)) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid email address.');
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
...@@ -75,32 +70,32 @@ class CEmailValidator extends Validator ...@@ -75,32 +70,32 @@ class CEmailValidator extends Validator
/** /**
* Validates a static value to see if it is a valid email. * Validates a static value to see if it is a valid email.
* Note that this method does not respect {@link allowEmpty} property. * Note that this method does not respect [[allowEmpty]] property.
* This method is provided so that you can call it directly without going through the model validation rule mechanism. * This method is provided so that you can call it directly without going through the model validation rule mechanism.
* @param mixed $value the value to be validated * @param mixed $value the value to be validated
* @return boolean whether the value is a valid email * @return boolean whether the value is a valid email
* @since 1.1.1
*/ */
public function validateValue($value) public function validateValue($value)
{ {
// make sure string length is limited to avoid DOS attacks // make sure string length is limited to avoid DOS attacks
$valid = is_string($value) && strlen($value) <= 254 && (preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value)); $valid = is_string($value) && strlen($value) <= 254 && (preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value));
if ($valid) if ($valid) {
$domain = rtrim(substr($value, strpos($value, '@') + 1), '>'); $domain = rtrim(substr($value, strpos($value, '@') + 1), '>');
if ($valid && $this->checkMX && function_exists('checkdnsrr')) }
if ($valid && $this->checkMX && function_exists('checkdnsrr')) {
$valid = checkdnsrr($domain, 'MX'); $valid = checkdnsrr($domain, 'MX');
if ($valid && $this->checkPort && function_exists('fsockopen')) }
if ($valid && $this->checkPort && function_exists('fsockopen')) {
$valid = fsockopen($domain, 25) !== false; $valid = fsockopen($domain, 25) !== false;
}
return $valid; return $valid;
} }
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
* @since 1.1.7
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
...@@ -110,8 +105,9 @@ class CEmailValidator extends Validator ...@@ -110,8 +105,9 @@ class CEmailValidator extends Validator
)); ));
$condition = "!value.match( {$this->pattern})"; $condition = "!value.match( {$this->pattern})";
if ($this->allowName) if ($this->allowName) {
$condition .= " && !value.match( {$this->fullPattern})"; $condition .= " && !value.match( {$this->fullPattern})";
}
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
......
...@@ -53,7 +53,7 @@ class CExistValidator extends Validator ...@@ -53,7 +53,7 @@ class CExistValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
...@@ -103,7 +103,7 @@ class CFileValidator extends Validator ...@@ -103,7 +103,7 @@ class CFileValidator extends Validator
/** /**
* Set the attribute and then validates using {@link validateFile}. * Set the attribute and then validates using {@link validateFile}.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -139,7 +139,7 @@ class CFileValidator extends Validator ...@@ -139,7 +139,7 @@ class CFileValidator extends Validator
/** /**
* Internally validates a file object. * Internally validates a file object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
* @param CUploadedFile $file uploaded file passed to check against a set of rules * @param CUploadedFile $file uploaded file passed to check against a set of rules
*/ */
...@@ -183,7 +183,7 @@ class CFileValidator extends Validator ...@@ -183,7 +183,7 @@ class CFileValidator extends Validator
/** /**
* Raises an error to inform end user about blank attribute. * Raises an error to inform end user about blank attribute.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function emptyAttribute($object, $attribute) public function emptyAttribute($object, $attribute)
......
...@@ -38,7 +38,7 @@ class CFilterValidator extends Validator ...@@ -38,7 +38,7 @@ class CFilterValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
...@@ -59,7 +59,7 @@ class CNumberValidator extends Validator ...@@ -59,7 +59,7 @@ class CNumberValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -97,7 +97,7 @@ class CNumberValidator extends Validator ...@@ -97,7 +97,7 @@ class CNumberValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
...@@ -43,7 +43,7 @@ class CRangeValidator extends Validator ...@@ -43,7 +43,7 @@ class CRangeValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -67,7 +67,7 @@ class CRangeValidator extends Validator ...@@ -67,7 +67,7 @@ class CRangeValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
<?php <?php
/** /**
* CRegularExpressionValidator class file. * RegularExpressionValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,15 +10,14 @@ ...@@ -10,15 +10,14 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CRegularExpressionValidator validates that the attribute value matches to the specified {@link pattern regular expression}. * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
* You may invert the validation logic with help of the {@link not} property (available since 1.1.5). *
* If [[not]] is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CRegularExpressionValidator.php 3120 2011-03-25 01:50:48Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CRegularExpressionValidator extends Validator class RegularExpressionValidator extends Validator
{ {
/** /**
* @var string the regular expression to be matched with * @var string the regular expression to be matched with
...@@ -31,26 +30,26 @@ class CRegularExpressionValidator extends Validator ...@@ -31,26 +30,26 @@ class CRegularExpressionValidator extends Validator
public $allowEmpty = true; public $allowEmpty = true;
/** /**
* @var boolean whether to invert the validation logic. Defaults to false. If set to true, * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
* the regular expression defined via {@link pattern} should NOT match the attribute value. * the regular expression defined via [[pattern]] should NOT match the attribute value.
* @since 1.1.5
**/ **/
public $not = false; public $not = false;
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
if ($this->pattern === null) }
throw new CException(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); if ($this->pattern === null) {
if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) throw new \yii\base\Exception(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.'));
{ }
if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.');
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
...@@ -58,16 +57,15 @@ class CRegularExpressionValidator extends Validator ...@@ -58,16 +57,15 @@ class CRegularExpressionValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
* @since 1.1.7
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
if ($this->pattern === null) if ($this->pattern === null) {
throw new CException(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.')); throw new \yii\base\Exception(Yii::t('yii', 'The "pattern" property must be specified with a valid regular expression.'));
}
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is invalid.');
$message = strtr($message, array( $message = strtr($message, array(
...@@ -79,15 +77,18 @@ class CRegularExpressionValidator extends Validator ...@@ -79,15 +77,18 @@ class CRegularExpressionValidator extends Validator
$delim = substr($pattern, 0, 1); $delim = substr($pattern, 0, 1);
$endpos = strrpos($pattern, $delim, 1); $endpos = strrpos($pattern, $delim, 1);
$flag = substr($pattern, $endpos + 1); $flag = substr($pattern, $endpos + 1);
if ($delim !== '/') if ($delim !== '/') {
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/'; $pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/';
else }
else {
$pattern = substr($pattern, 0, $endpos + 1); $pattern = substr($pattern, 0, $endpos + 1);
if (!empty($flag)) }
if (!empty($flag)) {
$pattern .= preg_replace('/[^igm]/', '', $flag); $pattern .= preg_replace('/[^igm]/', '', $flag);
}
return " return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) {
messages.push(" . json_encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
......
...@@ -37,85 +37,61 @@ class RequiredValidator extends Validator ...@@ -37,85 +37,61 @@ class RequiredValidator extends Validator
public $strict = false; public $strict = false;
/** /**
* Validates a value.
* @param mixed $value the value being validated.
* @return boolean whether the value is valid.
*/
public function validateValue($value)
{
if ($this->requiredValue !== null) {
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue)
{
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be {value}.',
array('{value}' => $this->requiredValue));
$this->addError($object, $attribute, $message);
}
}
elseif ($this->isEmpty($value, true)) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.');
$this->addError($object, $attribute, $message);
}
}
/**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->requiredValue !== null) if ($this->requiredValue === null) {
{ if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.');
{
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be {value}.',
array('{value}' => $this->requiredValue));
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
} }
elseif ($this->isEmpty($value, true)) else {
{ if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be "{requiredValue}".',
array('{requiredValue}' => $this->requiredValue));
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
} }
}
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
* @since 1.1.7
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
$message = $this->message; $message = $this->message;
if ($this->requiredValue !== null) if ($this->requiredValue !== null) {
{ if ($message === null) {
if ($message === null) $message = Yii::t('yii', '{attribute} must be "{requiredValue}".');
$message = Yii::t('yii', '{attribute} must be {value}.'); }
$message = strtr($message, array( $message = strtr($message, array(
'{value}' => $this->requiredValue,
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{requiredValue}' => $this->requiredValue,
)); ));
return " return "
if(value!=" . json_encode($this->requiredValue) . ") { if (value != " . json_encode($this->requiredValue) . ") {
messages.push(" . json_encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
} }
else else {
{ if ($message === null) {
if ($message === null)
$message = Yii::t('yii', '{attribute} cannot be blank.'); $message = Yii::t('yii', '{attribute} cannot be blank.');
}
$message = strtr($message, array( $message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
)); ));
return " return "
if($.trim(value)=='') { if($.trim(value) == '') {
messages.push(" . json_encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";
......
<?php <?php
/** /**
* CSafeValidator class file. * SafeValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,19 +10,17 @@ ...@@ -10,19 +10,17 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CSafeValidator marks the associated attributes to be safe for massive assignments. * SafeValidator marks the associated attributes to be safe for massive assignments.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CSafeValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.1
*/ */
class CSafeValidator extends Validator class SafeValidator extends Validator
{ {
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
...@@ -60,7 +60,7 @@ class CStringValidator extends Validator ...@@ -60,7 +60,7 @@ class CStringValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
...@@ -93,7 +93,7 @@ class CStringValidator extends Validator ...@@ -93,7 +93,7 @@ class CStringValidator extends Validator
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
......
...@@ -72,7 +72,7 @@ class CTypeValidator extends Validator ...@@ -72,7 +72,7 @@ class CTypeValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
...@@ -69,7 +69,7 @@ class CUniqueValidator extends Validator ...@@ -69,7 +69,7 @@ class CUniqueValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
<?php <?php
/** /**
* CUnsafeValidator class file. * UnsafeValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,25 +10,23 @@ ...@@ -10,25 +10,23 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CUnsafeValidator marks the associated attributes to be unsafe so that they cannot be massively assigned. * UnsafeValidator marks the associated attributes to be unsafe so that they cannot be massively assigned.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CUnsafeValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CUnsafeValidator extends Validator class UnsafeValidator extends Validator
{ {
/** /**
* @var boolean whether attributes listed with this validator should be considered safe for massive assignment. * @var boolean whether attributes listed with this validator should be considered safe for massive assignment.
* Defaults to false. * Defaults to false.
* @since 1.1.4
*/ */
public $safe = false; public $safe = false;
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
......
<?php <?php
/** /**
* CUrlValidator class file. * UrlValidator class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,32 +10,28 @@ ...@@ -10,32 +10,28 @@
namespace yii\validators; namespace yii\validators;
/** /**
* CUrlValidator validates that the attribute value is a valid http or https URL. * UrlValidator validates that the attribute value is a valid http or https URL.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CUrlValidator.php 3242 2011-05-28 14:31:04Z qiang.xue $ * @since 2.0
* @package system.validators
* @since 1.0
*/ */
class CUrlValidator extends Validator class UrlValidator extends Validator
{ {
/** /**
* @var string the regular expression used to validate the attribute value. * @var string the regular expression used to validate the attribute value.
* Since version 1.1.7 the pattern may contain a {schemes} token that will be replaced * The pattern may contain a `{schemes}` token that will be replaced
* by a regular expression which represents the {@see validSchemes}. * by a regular expression which represents the [[validSchemes]].
*/ */
public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i'; public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';
/** /**
* @var array list of URI schemes which should be considered valid. By default, http and https * @var array list of URI schemes which should be considered valid. By default, http and https
* are considered to be valid schemes. * are considered to be valid schemes.
* @since 1.1.7
**/ **/
public $validSchemes = array('http', 'https'); public $validSchemes = array('http', 'https');
/** /**
* @var string the default URI scheme. If the input doesn't contain the scheme part, the default * @var string the default URI scheme. If the input doesn't contain the scheme part, the default
* scheme will be prepended to it (thus changing the input). Defaults to null, meaning a URL must * scheme will be prepended to it (thus changing the input). Defaults to null, meaning a URL must
* contain the scheme part. * contain the scheme part.
* @since 1.1.7
**/ **/
public $defaultScheme; public $defaultScheme;
/** /**
...@@ -47,18 +43,19 @@ class CUrlValidator extends Validator ...@@ -47,18 +43,19 @@ class CUrlValidator extends Validator
/** /**
* Validates the attribute of the object. * Validates the attribute of the object.
* If there is any error, the error message is added to the object. * If there is any error, the error message is added to the object.
* @param CModel $object the object being validated * @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$value = $object->$attribute; $value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
if (($value = $this->validateValue($value)) !== false) }
if (($value = $this->validateValue($value)) !== false) {
$object->$attribute = $value; $object->$attribute = $value;
else }
{ else {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.');
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
...@@ -66,37 +63,39 @@ class CUrlValidator extends Validator ...@@ -66,37 +63,39 @@ class CUrlValidator extends Validator
/** /**
* Validates a static value to see if it is a valid URL. * Validates a static value to see if it is a valid URL.
* Note that this method does not respect {@link allowEmpty} property. * Note that this method does not respect [[allowEmpty]] property.
* This method is provided so that you can call it directly without going through the model validation rule mechanism. * This method is provided so that you can call it directly without going through the model validation rule mechanism.
* @param mixed $value the value to be validated * @param mixed $value the value to be validated
* @return mixed false if the the value is not a valid URL, otherwise the possibly modified value ({@see defaultScheme}) * @return mixed false if the the value is not a valid URL, otherwise the possibly modified value ({@see defaultScheme})
* @since 1.1.1
*/ */
public function validateValue($value) public function validateValue($value)
{ {
if (is_string($value) && strlen($value) < 2000) // make sure the length is limited to avoid DOS attacks // make sure the length is limited to avoid DOS attacks
{ if (is_string($value) && strlen($value) < 2000) {
if ($this->defaultScheme !== null && strpos($value, '://') === false) if ($this->defaultScheme !== null && strpos($value, '://') === false) {
$value = $this->defaultScheme . '://' . $value; $value = $this->defaultScheme . '://' . $value;
}
if (strpos($this->pattern, '{schemes}') !== false) if (strpos($this->pattern, '{schemes}') !== false) {
$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
else }
else {
$pattern = $this->pattern; $pattern = $this->pattern;
}
if (preg_match($pattern, $value)) if (preg_match($pattern, $value)) {
return $value; return $value;
} }
}
return false; return false;
} }
/** /**
* Returns the JavaScript needed for performing client-side validation. * Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see CActiveForm::enableClientValidation
* @since 1.1.7
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
......
...@@ -12,8 +12,9 @@ namespace yii\validators; ...@@ -12,8 +12,9 @@ namespace yii\validators;
/** /**
* Validator is the base class for all validators. * Validator is the base class for all validators.
* *
* Child classes may override the [[validateValue]] method to provide the actual * Child classes must override the [[validateAttribute]] method to provide the actual
* logic of performing data validation. * logic of performing data validation. Child classes may also override [[clientValidateAttribute]]
* to provide client-side validation support.
* *
* Validator defines the following properties that are common among concrete validators: * Validator defines the following properties that are common among concrete validators:
* *
...@@ -45,42 +46,39 @@ namespace yii\validators; ...@@ -45,42 +46,39 @@ namespace yii\validators;
* - `unsafe`: [[UnsafeValidator]] * - `unsafe`: [[UnsafeValidator]]
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $
* @package system.validators
* @since 2.0 * @since 2.0
*/ */
class Validator extends \yii\base\Component abstract class Validator extends \yii\base\Component
{ {
/** /**
* @var array list of built-in validators (name => class or configuration) * @var array list of built-in validators (name => class or configuration)
*/ */
public static $builtInValidators = array( public static $builtInValidators = array(
'required' => '\yii\validators\RequiredValidator', 'required' => '\yii\validators\RequiredValidator',
'filter' => '\yii\validators\FilterValidator',
'match' => '\yii\validators\RegularExpressionValidator', 'match' => '\yii\validators\RegularExpressionValidator',
'email' => '\yii\validators\EmailValidator', 'email' => '\yii\validators\EmailValidator',
'url' => '\yii\validators\UrlValidator', 'url' => '\yii\validators\UrlValidator',
'compare' => '\yii\validators\CompareValidator', 'safe' => '\yii\validators\SafeValidator',
'length' => '\yii\validators\StringValidator', 'unsafe' => '\yii\validators\UnsafeValidator',
'in' => '\yii\validators\RangeValidator',
'numerical' => '\yii\validators\NumberValidator',
'numerical' => '\yii\validators\NumberValidator',
'boolean' => '\yii\validators\BooleanValidator', 'boolean' => '\yii\validators\BooleanValidator',
'integer' => '\yii\validators\IntegerValidator', 'integer' => '\yii\validators\IntegerValidator',
'float' => '\yii\validators\FloatValidator', 'float' => '\yii\validators\FloatValidator',
'string' => '\yii\validators\StringValidator', 'string' => '\yii\validators\StringValidator',
'date' => '\yii\validators\DateValidator', 'date' => '\yii\validators\DateValidator',
'file' => '\yii\validators\FileValidator',
'filter' => '\yii\validators\FilterValidator',
'compare' => '\yii\validators\CompareValidator',
'length' => '\yii\validators\StringValidator',
'in' => '\yii\validators\RangeValidator',
'captcha' => '\yii\validators\CaptchaValidator', 'captcha' => '\yii\validators\CaptchaValidator',
'type' => '\yii\validators\TypeValidator', 'type' => '\yii\validators\TypeValidator',
'file' => '\yii\validators\FileValidator',
'default' => '\yii\validators\DefaultValueValidator', 'default' => '\yii\validators\DefaultValueValidator',
'unique' => '\yii\validators\UniqueValidator', 'unique' => '\yii\validators\UniqueValidator',
'exist' => '\yii\validators\ExistValidator', 'exist' => '\yii\validators\ExistValidator',
'safe' => '\yii\validators\SafeValidator',
'unsafe' => '\yii\validators\UnsafeValidator',
); );
/** /**
...@@ -117,31 +115,12 @@ class Validator extends \yii\base\Component ...@@ -117,31 +115,12 @@ class Validator extends \yii\base\Component
public $enableClientValidation = true; public $enableClientValidation = true;
/** /**
* Validates a value.
* Child classes should override this method to implement the actual validation logic.
* The default implementation simply returns true.
* @param mixed $value the value being validated.
* @return boolean whether the value is valid.
*/
public function validateValue($value)
{
return true;
}
/**
* Validates a single attribute. * Validates a single attribute.
* The default implementation will call [[validateValue]] to determine if * Child classes must implement this method to provide the actual validation logic.
* the attribute value is valid or not. If not, the [[message|error message]]
* will be added to the model object.
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
*/ */
public function validateAttribute($object, $attribute) abstract public function validateAttribute($object, $attribute);
{
if (!$this->validateValue($object->$attribute)) {
$this->addError($object, $attribute, $this->message);
}
}
/** /**
* Creates a validator object. * Creates a validator object.
......
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