Yii2: UniqueValidator misleading error message for multiple targetAttribute's

Created on 11 Apr 2016  路  9Comments  路  Source: yiisoft/yii2

When UniqueValidator is setup like this:

 // a1 and a2 need to be unique together, and they both will receive error message
 [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]

and the pair of fields fails unique check the validator produces two separate error messages that are completely wrong and misleading:

a1 "123" has already been taken.
a2 "456" has already been taken.

The actual error messages should have been along the lines:

The combination ("123", "456") of (a1, a2) has already been taken.
The combination ("123", "456") of (a1, a2) has already been taken.

| Yii version | 2.0.8-dev
| PHP version | 7.0.1
| Operating system | Windows

ready for adoption enhancement

Most helpful comment

Thank you for the report. Would you like to submit a PR?

All 9 comments

Thank you for the report. Would you like to submit a PR?

@SilverFire

Would you like to submit a PR?

Here is one: https://github.com/yiisoft/yii2/pull/11323. Code is fixed but since a new error message was introduced it will need translation to many languages which I cannot do.

it bad idea, your code execute 2 equals query, for generate two errors

it bad idea, your code execute 2 equals query, for generate two errors

After the fix you will use the unique validator only on a single field (out of all the fields constituting the combo) - the field you want the error message to show on. For example:

 // a1 and a2 need to be unique together, but only a1 will receive error message
 ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]

This way you get single query and single error message.

I'm not sure if this has found it's way into the dev branch yet, but I have just rolled back to 2.0.8 due to an introduced bug in 2.0.9-dev. It seems similar to what you are discussing here. The issue i had was for this configuration:

 // a1 and a2 need to be unique together, and they both will receive error message
 [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]

The behavior was the error was only showing under a2, not both a1 and a2.

Likewise, with this configuration

[['a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]

The error message was still displayed under a1.

Rolling back to 2.0.8 returned expected behavior.

@enigmatix
What's discussed here has not been merged into any Yii2 branch yet, the bug you found is different and you should report it separately. Once changes in here are merged, the configuration:

 // a1 and a2 need to be unique together, and they both will receive error message
 [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]

is still supposed to produce two error messages (for a1 and for a2) as before.

Rolling back to 2.0.8 returned expected behavior.

@enigmatix Uniquevalidator has not been touched since 2.0.8, could you re-verify the issue and report it separately if it still exists?

I also noticed message attribute will be ignored if you set multiple target attributes:
['a1', 'unique', 'targetAttribute' => ['a1', 'a2'], 'message' => 'Custom message']

The message that appeared was: The combination ... has already been taken. Did anyone experience this issue? Is it related to this one?

I also noticed message attribute will be ignored if you set multiple target attributes:
['a1', 'unique', 'targetAttribute' => ['a1', 'a2'], 'message' => 'Custom message']

Since Yii2 2.0.9 when you set multiple target attributes comboNotUnique is used to customize error message instead of message.

Was this page helpful?
0 / 5 - 0 ratings