Commit d13d274d by Qiang Xue

renamed $object to $model

parent a0bd2033
......@@ -67,13 +67,13 @@ class BooleanValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$options = [
'trueValue' => $this->trueValue,
'falseValue' => $this->falseValue,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
'true' => $this->trueValue,
'false' => $this->falseValue,
], Yii::$app->language),
......
......@@ -117,11 +117,11 @@ class CompareValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
......@@ -129,12 +129,12 @@ class CompareValidator extends Validator
$compareLabel = $compareValue = $this->compareValue;
} else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = $object->$compareAttribute;
$compareLabel = $object->getAttributeLabel($compareAttribute);
$compareValue = $model->$compareAttribute;
$compareLabel = $model->getAttributeLabel($compareAttribute);
}
if (!$this->compareValues($this->operator, $this->type, $value, $compareValue)) {
$this->addError($object, $attribute, $this->message, [
$this->addError($model, $attribute, $this->message, [
'compareAttribute' => $compareLabel,
'compareValue' => $compareValue,
]);
......@@ -201,7 +201,7 @@ class CompareValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$options = [
'operator' => $this->operator,
......@@ -213,8 +213,8 @@ class CompareValidator extends Validator
$compareValue = $this->compareValue;
} else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = $object->getAttributeLabel($compareAttribute);
$options['compareAttribute'] = Html::getInputId($object, $compareAttribute);
$compareValue = $model->getAttributeLabel($compareAttribute);
$options['compareAttribute'] = Html::getInputId($model, $compareAttribute);
}
if ($this->skipOnEmpty) {
......@@ -222,7 +222,7 @@ class CompareValidator extends Validator
}
$options['message'] = Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
'compareAttribute' => $compareValue,
'compareValue' => $compareValue,
], Yii::$app->language);
......
......@@ -94,14 +94,14 @@ class DateValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
$timestamp = $this->parseDateValue($value);
if ($timestamp === false) {
$this->addError($object, $attribute, $this->message, []);
$this->addError($model, $attribute, $this->message, []);
} elseif ($this->timestampAttribute !== null) {
$object->{$this->timestampAttribute} = $timestamp;
$model->{$this->timestampAttribute} = $timestamp;
}
}
......
......@@ -41,13 +41,13 @@ class DefaultValueValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
if ($this->isEmpty($object->$attribute)) {
if ($this->isEmpty($model->$attribute)) {
if ($this->value instanceof \Closure) {
$object->$attribute = call_user_func($this->value, $object, $attribute);
$model->$attribute = call_user_func($this->value, $model, $attribute);
} else {
$object->$attribute = $this->value;
$model->$attribute = $this->value;
}
}
}
......
......@@ -92,14 +92,14 @@ class EmailValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$options = [
'pattern' => new JsExpression($this->pattern),
'fullPattern' => new JsExpression($this->fullPattern),
'allowName' => $this->allowName,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language),
'enableIDN' => (boolean) $this->enableIDN,
];
......
......@@ -81,7 +81,7 @@ class ExistValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
......@@ -91,31 +91,31 @@ class ExistValidator extends Validator
}
$params = [];
foreach ($targetAttribute as $k => $v) {
$params[$v] = is_integer($k) ? $object->$v : $object->$k;
$params[$v] = is_integer($k) ? $model->$v : $model->$k;
}
} else {
$params = [$targetAttribute => $object->$attribute];
$params = [$targetAttribute => $model->$attribute];
}
if (!$this->allowArray) {
foreach ($params as $value) {
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
}
}
$targetClass = $this->targetClass === null ? get_class($object) : $this->targetClass;
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass;
$query = $this->createQuery($targetClass, $params);
if (is_array($object->$attribute)) {
if ($query->count("DISTINCT [[$targetAttribute]]") != count($object->$attribute)) {
$this->addError($object, $attribute, $this->message);
if (is_array($model->$attribute)) {
if ($query->count("DISTINCT [[$targetAttribute]]") != count($model->$attribute)) {
$this->addError($model, $attribute, $this->message);
}
} elseif (!$query->exists()) {
$this->addError($object, $attribute, $this->message);
$this->addError($model, $attribute, $this->message);
}
}
......
......@@ -166,12 +166,12 @@ class FileValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
if ($this->maxFiles > 1) {
$files = $object->$attribute;
$files = $model->$attribute;
if (!is_array($files)) {
$this->addError($object, $attribute, $this->uploadRequired);
$this->addError($model, $attribute, $this->uploadRequired);
return;
}
......@@ -180,24 +180,24 @@ class FileValidator extends Validator
unset($files[$i]);
}
}
$object->$attribute = array_values($files);
$model->$attribute = array_values($files);
if (empty($files)) {
$this->addError($object, $attribute, $this->uploadRequired);
$this->addError($model, $attribute, $this->uploadRequired);
}
if (count($files) > $this->maxFiles) {
$this->addError($object, $attribute, $this->tooMany, ['limit' => $this->maxFiles]);
$this->addError($model, $attribute, $this->tooMany, ['limit' => $this->maxFiles]);
} else {
foreach ($files as $file) {
$result = $this->validateValue($file);
if (!empty($result)) {
$this->addError($object, $attribute, $result[0], $result[1]);
$this->addError($model, $attribute, $result[0], $result[1]);
}
}
}
} else {
$result = $this->validateValue($object->$attribute);
$result = $this->validateValue($model->$attribute);
if (!empty($result)) {
$this->addError($object, $attribute, $result[0], $result[1]);
$this->addError($model, $attribute, $result[0], $result[1]);
}
}
}
......@@ -334,22 +334,22 @@ class FileValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
ValidationAsset::register($view);
$options = $this->getClientOptions($object, $attribute);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.file(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
}
/**
* Returns the client side validation options.
* @param \yii\base\Model $object the model being validated
* @param \yii\base\Model $model the model being validated
* @param string $attribute the attribute name being validated
* @return array the client side validation options
*/
protected function getClientOptions($object, $attribute)
protected function getClientOptions($model, $attribute)
{
$label = $object->getAttributeLabel($attribute);
$label = $model->getAttributeLabel($attribute);
$options = [];
if ($this->message !== null) {
......
......@@ -65,11 +65,11 @@ class FilterValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
if (!$this->skipOnArray || !is_array($value)) {
$object->$attribute = call_user_func($this->filter, $value);
$model->$attribute = call_user_func($this->filter, $value);
}
}
}
......@@ -162,21 +162,21 @@ class ImageValidator extends FileValidator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
ValidationAsset::register($view);
$options = $this->getClientOptions($object, $attribute);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.image(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ', deferred);';
}
/**
* @inheritdoc
*/
protected function getClientOptions($object, $attribute)
protected function getClientOptions($model, $attribute)
{
$options = parent::getClientOptions($object, $attribute);
$options = parent::getClientOptions($model, $attribute);
$label = $object->getAttributeLabel($attribute);
$label = $model->getAttributeLabel($attribute);
if ($this->notImage !== null) {
$options['notImage'] = Yii::$app->getI18n()->format($this->notImage, [
......
......@@ -58,11 +58,11 @@ class InlineValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$method = $this->method;
if (is_string($method)) {
$method = [$object, $method];
$method = [$model, $method];
}
call_user_func($method, $attribute, $this->params);
}
......@@ -70,12 +70,12 @@ class InlineValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->clientValidate !== null) {
$method = $this->clientValidate;
if (is_string($method)) {
$method = [$object, $method];
$method = [$model, $method];
}
return call_user_func($method, $attribute, $this->params);
......
......@@ -75,22 +75,22 @@ class NumberValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, $this->message);
$this->addError($model, $attribute, $this->message);
return;
}
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
if (!preg_match($pattern, "$value")) {
$this->addError($object, $attribute, $this->message);
$this->addError($model, $attribute, $this->message);
}
if ($this->min !== null && $value < $this->min) {
$this->addError($object, $attribute, $this->tooSmall, ['min' => $this->min]);
$this->addError($model, $attribute, $this->tooSmall, ['min' => $this->min]);
}
if ($this->max !== null && $value > $this->max) {
$this->addError($object, $attribute, $this->tooBig, ['max' => $this->max]);
$this->addError($model, $attribute, $this->tooBig, ['max' => $this->max]);
}
}
......@@ -117,9 +117,9 @@ class NumberValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$label = $object->getAttributeLabel($attribute);
$label = $model->getAttributeLabel($attribute);
$options = [
'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern),
......
......@@ -79,7 +79,7 @@ class RangeValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$range = [];
foreach ($this->range as $value) {
......@@ -89,7 +89,7 @@ class RangeValidator extends Validator
'range' => $range,
'not' => $this->not,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language),
];
if ($this->skipOnEmpty) {
......
......@@ -62,7 +62,7 @@ class RegularExpressionValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$pattern = $this->pattern;
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
......@@ -82,7 +82,7 @@ class RegularExpressionValidator extends Validator
'pattern' => new JsExpression($pattern),
'not' => $this->not,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language),
];
if ($this->skipOnEmpty) {
......
......@@ -86,7 +86,7 @@ class RequiredValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$options = [];
if ($this->requiredValue !== null) {
......@@ -102,7 +102,7 @@ class RequiredValidator extends Validator
}
$options['message'] = Yii::$app->getI18n()->format($options['message'], [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language);
ValidationAsset::register($view);
......
......@@ -18,7 +18,7 @@ class SafeValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
}
}
......@@ -96,12 +96,12 @@ class StringValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
if (!is_string($value)) {
$this->addError($object, $attribute, $this->message);
$this->addError($model, $attribute, $this->message);
return;
}
......@@ -109,13 +109,13 @@ class StringValidator extends Validator
$length = mb_strlen($value, $this->encoding);
if ($this->min !== null && $length < $this->min) {
$this->addError($object, $attribute, $this->tooShort, ['min' => $this->min]);
$this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
}
if ($this->max !== null && $length > $this->max) {
$this->addError($object, $attribute, $this->tooLong, ['max' => $this->max]);
$this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
}
if ($this->length !== null && $length !== $this->length) {
$this->addError($object, $attribute, $this->notEqual, ['length' => $this->length]);
$this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
}
}
......@@ -146,9 +146,9 @@ class StringValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
$label = $object->getAttributeLabel($attribute);
$label = $model->getAttributeLabel($attribute);
$options = [
'message' => Yii::$app->getI18n()->format($this->message, [
......
......@@ -74,24 +74,24 @@ class UniqueValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
/* @var $targetClass ActiveRecordInterface */
$targetClass = $this->targetClass === null ? get_class($object) : $this->targetClass;
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass;
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
if (is_array($targetAttribute)) {
$params = [];
foreach ($targetAttribute as $k => $v) {
$params[$v] = is_integer($k) ? $object->$v : $object->$k;
$params[$v] = is_integer($k) ? $model->$v : $model->$k;
}
} else {
$params = [$targetAttribute => $object->$attribute];
$params = [$targetAttribute => $model->$attribute];
}
foreach ($params as $value) {
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
......@@ -106,14 +106,14 @@ class UniqueValidator extends Validator
$query->andWhere($this->filter);
}
if (!$object instanceof ActiveRecordInterface || $object->getIsNewRecord()) {
// if current $object isn't in the database yet then it's OK just to call exists()
if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord()) {
// if current $model isn't in the database yet then it's OK just to call exists()
$exists = $query->exists();
} else {
// if current $object is in the database already we can't use exists()
/* @var $objects ActiveRecordInterface[] */
$objects = $query->limit(2)->all();
$n = count($objects);
// if current $model is in the database already we can't use exists()
/* @var $models ActiveRecordInterface[] */
$models = $query->limit(2)->all();
$n = count($models);
if ($n === 1) {
$keys = array_keys($params);
$pks = $targetClass::primaryKey();
......@@ -121,10 +121,10 @@ class UniqueValidator extends Validator
sort($pks);
if ($keys === $pks) {
// primary key is modified and not unique
$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
$exists = $model->getOldPrimaryKey() != $model->getPrimaryKey();
} else {
// non-primary key, need to exclude the current record based on PK
$exists = $objects[0]->getPrimaryKey() != $object->getOldPrimaryKey();
$exists = $models[0]->getPrimaryKey() != $model->getOldPrimaryKey();
}
} else {
$exists = $n > 1;
......@@ -132,7 +132,7 @@ class UniqueValidator extends Validator
}
if ($exists) {
$this->addError($object, $attribute, $this->message);
$this->addError($model, $attribute, $this->message);
}
}
}
......@@ -66,14 +66,14 @@ class UrlValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($object, $attribute)
public function validateAttribute($model, $attribute)
{
$value = $object->$attribute;
$value = $model->$attribute;
$result = $this->validateValue($value);
if (!empty($result)) {
$this->addError($object, $attribute, $result[0], $result[1]);
$this->addError($model, $attribute, $result[0], $result[1]);
} elseif ($this->defaultScheme !== null && strpos($value, '://') === false) {
$object->$attribute = $this->defaultScheme . '://' . $value;
$model->$attribute = $this->defaultScheme . '://' . $value;
}
}
......@@ -111,7 +111,7 @@ class UrlValidator extends Validator
/**
* @inheritdoc
*/
public function clientValidateAttribute($object, $attribute, $view)
public function clientValidateAttribute($model, $attribute, $view)
{
if (strpos($this->pattern, '{schemes}') !== false) {
$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
......@@ -122,7 +122,7 @@ class UrlValidator extends Validator
$options = [
'pattern' => new JsExpression($pattern),
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language),
'enableIDN' => (boolean) $this->enableIDN,
];
......
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