Browse Source

Clientvalidation {value} was not what has been validated

also fixed some inconsistency between client and server validation.
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
266f4f9843
  1. 37
      framework/yii/assets/yii.validation.js
  2. 1
      framework/yii/validators/BooleanValidator.php
  3. 3
      framework/yii/validators/CompareValidator.php
  4. 1
      framework/yii/validators/EmailValidator.php
  5. 4
      framework/yii/validators/NumberValidator.php
  6. 1
      framework/yii/validators/RangeValidator.php
  7. 1
      framework/yii/validators/RegularExpressionValidator.php
  8. 1
      framework/yii/validators/RequiredValidator.php
  9. 5
      framework/yii/validators/StringValidator.php
  10. 1
      framework/yii/validators/UrlValidator.php

37
framework/yii/assets/yii.validation.js

@ -16,6 +16,10 @@ yii.validation = (function ($) {
|| value === '' || trim && $.trim(value) === ''; || value === '' || trim && $.trim(value) === '';
}; };
var addMessage = function (messages, message, value) {
messages.push(message.replace(/\{value\}/g, value));
};
return { return {
required: function (value, messages, options) { required: function (value, messages, options) {
var valid = false; var valid = false;
@ -28,7 +32,7 @@ yii.validation = (function ($) {
} }
if (!valid) { if (!valid) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -40,7 +44,7 @@ yii.validation = (function ($) {
|| options.strict && (value === options.trueValue || value === options.falseValue); || options.strict && (value === options.trueValue || value === options.falseValue);
if (!valid) { if (!valid) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -50,18 +54,18 @@ yii.validation = (function ($) {
} }
if (typeof value !== 'string') { if (typeof value !== 'string') {
messages.push(options.message); addMessage(messages, options.message, value);
return; return;
} }
if (options.min !== undefined && value.length < options.min) { if (options.min !== undefined && value.length < options.min) {
messages.push(options.tooShort); addMessage(messages, options.tooShort, value);
} }
if (options.max !== undefined && value.length > options.max) { if (options.max !== undefined && value.length > options.max) {
messages.push(options.tooLong); addMessage(messages, options.tooLong, value);
} }
if (options.is !== undefined && value.length != options.is) { if (options.is !== undefined && value.length != options.is) {
messages.push(options.is); addMessage(messages, options.is, value);
} }
}, },
@ -71,15 +75,15 @@ yii.validation = (function ($) {
} }
if (typeof value === 'string' && !value.match(options.pattern)) { if (typeof value === 'string' && !value.match(options.pattern)) {
messages.push(options.message); addMessage(messages, options.message, value);
return; return;
} }
if (options.min !== undefined && value < options.min) { if (options.min !== undefined && value < options.min) {
messages.push(options.tooSmall); addMessage(messages, options.tooSmall, value);
} }
if (options.max !== undefined && value > options.max) { if (options.max !== undefined && value > options.max) {
messages.push(options.tooBig); addMessage(messages, options.tooBig, value);
} }
}, },
@ -91,7 +95,7 @@ yii.validation = (function ($) {
|| options.not && $.inArray(value, options.range) == -1; || options.not && $.inArray(value, options.range) == -1;
if (!valid) { if (!valid) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -101,7 +105,7 @@ yii.validation = (function ($) {
} }
if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) { if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
messages.push(options.message) addMessage(messages, options.message, value);
} }
}, },
@ -123,7 +127,7 @@ yii.validation = (function ($) {
} }
if (!valid || !(value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern)))) { if (!valid || !(value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern)))) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -149,7 +153,7 @@ yii.validation = (function ($) {
} }
if (!valid || !value.match(options.pattern)) { if (!valid || !value.match(options.pattern)) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -170,7 +174,7 @@ yii.validation = (function ($) {
h += v.charCodeAt(i); h += v.charCodeAt(i);
} }
if (h != hash) { if (h != hash) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
}, },
@ -210,10 +214,13 @@ yii.validation = (function ($) {
case '<=': case '<=':
valid = value <= compareValue; valid = value <= compareValue;
break; break;
default:
valid = false;
break;
} }
if (!valid) { if (!valid) {
messages.push(options.message); addMessage(messages, options.message, value);
} }
} }
}; };

1
framework/yii/validators/BooleanValidator.php

@ -90,7 +90,6 @@ class BooleanValidator extends Validator
'falseValue' => $this->falseValue, 'falseValue' => $this->falseValue,
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
'{true}' => $this->trueValue, '{true}' => $this->trueValue,
'{false}' => $this->falseValue, '{false}' => $this->falseValue,
))), ))),

3
framework/yii/validators/CompareValidator.php

@ -170,6 +170,7 @@ class CompareValidator extends Validator
case '>=': return $value >= $this->compareValue; case '>=': return $value >= $this->compareValue;
case '<': return $value < $this->compareValue; case '<': return $value < $this->compareValue;
case '<=': return $value <= $this->compareValue; case '<=': return $value <= $this->compareValue;
default: return false;
} }
} }
@ -201,7 +202,7 @@ class CompareValidator extends Validator
$options['message'] = Html::encode(strtr($this->message, array( $options['message'] = Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{compareAttribute}' => $compareValue,
'{compareValue}' => $compareValue, '{compareValue}' => $compareValue,
))); )));

1
framework/yii/validators/EmailValidator.php

@ -131,7 +131,6 @@ class EmailValidator extends Validator
'allowName' => $this->allowName, 'allowName' => $this->allowName,
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
))), ))),
'enableIDN' => (boolean)$this->enableIDN, 'enableIDN' => (boolean)$this->enableIDN,
); );

4
framework/yii/validators/NumberValidator.php

@ -121,13 +121,11 @@ class NumberValidator extends Validator
public function clientValidateAttribute($object, $attribute, $view) public function clientValidateAttribute($object, $attribute, $view)
{ {
$label = $object->getAttributeLabel($attribute); $label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
$options = array( $options = array(
'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern), 'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern),
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
))), ))),
); );
@ -135,7 +133,6 @@ class NumberValidator extends Validator
$options['min'] = $this->min; $options['min'] = $this->min;
$options['tooSmall'] = Html::encode(strtr($this->tooSmall, array( $options['tooSmall'] = Html::encode(strtr($this->tooSmall, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min, '{min}' => $this->min,
))); )));
} }
@ -143,7 +140,6 @@ class NumberValidator extends Validator
$options['max'] = $this->max; $options['max'] = $this->max;
$options['tooBig'] = Html::encode(strtr($this->tooBig, array( $options['tooBig'] = Html::encode(strtr($this->tooBig, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max, '{max}' => $this->max,
))); )));
} }

1
framework/yii/validators/RangeValidator.php

@ -96,7 +96,6 @@ class RangeValidator extends Validator
'not' => $this->not, 'not' => $this->not,
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
))), ))),
); );
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {

1
framework/yii/validators/RegularExpressionValidator.php

@ -103,7 +103,6 @@ class RegularExpressionValidator extends Validator
'not' => $this->not, 'not' => $this->not,
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
))), ))),
); );
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {

1
framework/yii/validators/RequiredValidator.php

@ -123,7 +123,6 @@ class RequiredValidator extends Validator
$options['message'] = Html::encode(strtr($options['message'], array( $options['message'] = Html::encode(strtr($options['message'], array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
))); )));
ValidationAsset::register($view); ValidationAsset::register($view);

5
framework/yii/validators/StringValidator.php

@ -149,12 +149,10 @@ class StringValidator extends Validator
public function clientValidateAttribute($object, $attribute, $view) public function clientValidateAttribute($object, $attribute, $view)
{ {
$label = $object->getAttributeLabel($attribute); $label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
$options = array( $options = array(
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
))), ))),
); );
@ -162,7 +160,6 @@ class StringValidator extends Validator
$options['min'] = $this->min; $options['min'] = $this->min;
$options['tooShort'] = Html::encode(strtr($this->tooShort, array( $options['tooShort'] = Html::encode(strtr($this->tooShort, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min, '{min}' => $this->min,
))); )));
} }
@ -170,7 +167,6 @@ class StringValidator extends Validator
$options['max'] = $this->max; $options['max'] = $this->max;
$options['tooLong'] = Html::encode(strtr($this->tooLong, array( $options['tooLong'] = Html::encode(strtr($this->tooLong, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max, '{max}' => $this->max,
))); )));
} }
@ -178,7 +174,6 @@ class StringValidator extends Validator
$options['is'] = $this->length; $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,
'{length}' => $this->is, '{length}' => $this->is,
))); )));
} }

1
framework/yii/validators/UrlValidator.php

@ -132,7 +132,6 @@ class UrlValidator extends Validator
'pattern' => new JsExpression($pattern), 'pattern' => new JsExpression($pattern),
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
))), ))),
'enableIDN' => (boolean)$this->enableIDN, 'enableIDN' => (boolean)$this->enableIDN,
); );

Loading…
Cancel
Save