mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Merge pull request #12305 from rob006/fix-11323-bc
Fix BC break for `UniqueValidator` custom message
This commit is contained in:
@ -53,6 +53,7 @@ Yii Framework 2 Change Log
|
||||
- Bug #11973: Fixed `yii\helpers\BaseHtml::getAttributeValue()` to work with `items[]` notation correctly (silverfire)
|
||||
- Bug #12100: Fixed `yii\filters\HttpCache` was sending an empty Pragma header (sergeymakinen)
|
||||
- Bug #12107: Fixed REST Serializer to validate input for 'expand' and 'fields' parameter, which crashed on array input (njspok, cebe)
|
||||
- Bug #12152: Fixed BC break for `UniqueValidator` custom message when validating multiple attributes (rob006)
|
||||
- Enh #12230: Allows BaseHtml::activeListInput to override the field value (RangelReale)
|
||||
- Bug #12331: Fixed bug with incorrect currency formatter output, when `$thousandSeparator` was explicitly set (cebe)
|
||||
- Bug #11347: Fixed `yii\widgets\Pjax::registerClientScript()` to pass custom `container` to the PJAX JS plugin (silverfire)
|
||||
|
@ -60,12 +60,24 @@ class UniqueValidator extends Validator
|
||||
*/
|
||||
public $filter;
|
||||
/**
|
||||
* @var string the user-defined error message used when [[targetAttribute]] is an array. It may contain the following placeholders:
|
||||
* @var string the user-defined error message. When validating single attribute, it may contain
|
||||
* the following placeholders which will be replaced accordingly by the validator:
|
||||
*
|
||||
* - `{attribute}`: the label of the attribute being validated
|
||||
* - `{value}`: the value of the attribute being validated
|
||||
*
|
||||
* When validating mutliple attributes, it may contain the following placeholders:
|
||||
*
|
||||
* - `{attributes}`: the labels of the attributes being validated.
|
||||
* - `{values}`: the values of the attributes being validated.
|
||||
*
|
||||
*/
|
||||
public $message;
|
||||
/**
|
||||
* @var string
|
||||
* @since 2.0.9
|
||||
* @deprecated Deprecated since version 2.0.10, to be removed in 2.1. Use [[message]] property
|
||||
* to setup custom message for multiple target attributes.
|
||||
*/
|
||||
public $comboNotUnique;
|
||||
|
||||
@ -76,11 +88,18 @@ class UniqueValidator extends Validator
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
if ($this->message === null) {
|
||||
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
|
||||
if ($this->message !== null) {
|
||||
return;
|
||||
}
|
||||
if (is_array($this->targetAttribute) && count($this->targetAttribute) > 1) {
|
||||
// fallback for deprecated `comboNotUnique` property - use it as message if is set
|
||||
if ($this->comboNotUnique === null) {
|
||||
$this->comboNotUnique = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.');
|
||||
$this->message = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.');
|
||||
} else {
|
||||
$this->message = $this->comboNotUnique;
|
||||
}
|
||||
} else {
|
||||
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,7 +191,7 @@ class UniqueValidator extends Validator
|
||||
$valueCombo[] = '"' . $model->$key . '"';
|
||||
}
|
||||
}
|
||||
$this->addError($model, $attribute, $this->comboNotUnique, [
|
||||
$this->addError($model, $attribute, $this->message, [
|
||||
'attributes' => Inflector::sentence($attributeCombo),
|
||||
'values' => implode('-', $valueCombo)
|
||||
]);
|
||||
|
@ -29,6 +29,42 @@ abstract class UniqueValidatorTest extends DatabaseTestCase
|
||||
$this->assertTrue(is_string($val->message));
|
||||
}
|
||||
|
||||
public function testCustomMessage()
|
||||
{
|
||||
// single attribute
|
||||
$customError = 'Custom message for Id with value "1"';
|
||||
$validator = new UniqueValidator([
|
||||
'message' => 'Custom message for {attribute} with value "{value}"',
|
||||
]);
|
||||
$model = new Order();
|
||||
$model->id = 1;
|
||||
$validator->validateAttribute($model, 'id');
|
||||
$this->assertTrue($model->hasErrors('id'));
|
||||
$this->assertEquals($customError, $model->getFirstError('id'));
|
||||
|
||||
// multiple attributes
|
||||
$customError = 'Custom message for Order Id and Item Id with values "1"-"1"';
|
||||
$validator = new UniqueValidator([
|
||||
'targetAttribute' => ['order_id', 'item_id'],
|
||||
'message' => 'Custom message for {attributes} with values {values}',
|
||||
]);
|
||||
$model = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
|
||||
$model->item_id = 1;
|
||||
$validator->validateAttribute($model, 'order_id');
|
||||
$this->assertTrue($model->hasErrors('order_id'));
|
||||
$this->assertEquals($customError, $model->getFirstError('order_id'));
|
||||
|
||||
// fallback for deprecated `comboNotUnique` - should be removed on 2.1.0
|
||||
$validator = new UniqueValidator([
|
||||
'targetAttribute' => ['order_id', 'item_id'],
|
||||
'comboNotUnique' => 'Custom message for {attributes} with values {values}',
|
||||
]);
|
||||
$model->clearErrors();
|
||||
$validator->validateAttribute($model, 'order_id');
|
||||
$this->assertTrue($model->hasErrors('order_id'));
|
||||
$this->assertEquals($customError, $model->getFirstError('order_id'));
|
||||
}
|
||||
|
||||
public function testValidateInvalidAttribute()
|
||||
{
|
||||
$validator = new UniqueValidator();
|
||||
|
Reference in New Issue
Block a user