Under Yii 2,[[yii\validators\UniqueValidator|unique(唯一性)]], uniqueness of the combined field, customizing the error message in the common model logic layer
1. The model class file in the /common/models directory is only allowed to be generated by the GII tool, which is the common model data layer, \common\models\configcolumn.php, the relevant verification rules of the model
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['group_id', 'code', 'name'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['group_id', 'code', 'name'], 'string', 'max' => 32],
[['group_id', 'code'], 'unique', 'targetAttribute' => ['group_id', 'code']],
[['group_id', 'name'], 'unique', 'targetAttribute' => ['group_id', 'name']],
];
}
2. The model class file in the /common/logics directory is related to business logic, which is inherited to /common/models The data layer is the common model logic layer, \logics\configcolumn.php, the relevant verification rules of the model
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
];
$parentRules = parent::rules();
return ArrayHelper::merge($rules, $parentRules);
}
3. Posthttp://api.pcs-api.localhost/v1/config-columns?login_id=e56db1b43546a110431ac38409ed8e9e&login_tid=ccffd87b68171e8cb6c19f4bb8ccd775422 response
{
"code": 20004,
"message": "Data validation failed: The combination \"015ce30b116ce86058fa6ab4fea4ac63\"-\"wxcq\" of Group ID and Code has already been taken."
}
{
"code": 20004,
"message": "数据验证失败:The combination \"015ce30b116ce86058fa6ab4fea4ac63\"-\"wxcq\" of 租户ID and 栏目代码 has already been taken."
}
4. Analyze the source code, \vendor\yiisoft\yii2\validators\UniqueValidator.php
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->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.');
}
5,the combination {values} of {attributes} has already been taken.It does not exist in the Chinese simplified language pack that comes with the framework, as shown in Figure 1
6,the combination {values} of {attributes} has already been taken.It exists in the English language pack that comes with the framework, but the source language is not translated, as shown in Figure 2
7. The model class file in the /common/models directory is only allowed to be generated by the GII tool, and it is not allowed to edit again. The rules() of \common\models\configColumn.php is changed, and \logics\configcolumn.php also needs to be adjusted accordingly.
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
[['group_id', 'code'], 'unique', 'targetAttribute' => ['group_id', 'code'], 'message' => Yii::t('app', 'The combination {values} of {attributes} has already been taken.')],
[['group_id', 'name'], 'unique', 'targetAttribute' => ['group_id', 'name'], 'message' => Yii::t('app', 'The combination {values} of {attributes} has already been taken.')],
];
$parentRules = parent::rules();
unset($parentRules[3], $parentRules[4]);
return ArrayHelper::merge($rules, $parentRules);
}
8. Edit \common\messages\zh-cn\app.php
'组合{attributes}的值"{values}"已经被占用了。',
];
9. Edit \api\messages\zh-cn\app.php
'组合{attributes}的值"{values}"已经被占用了。',
];
10. Posthttp://api.pcs-api.localhost/v1/config-columns?login_id=e56db1b43546a110431ac38409ed8e9e&login_tid=ccffd87b68171e8cb6c19f4bb8ccd775, 422 responses, as shown in Figure 3
{
"code": 20004,
"message": "Data validation failed: The combination \"015ce30b116ce86058fa6ab4fea4ac63\"-\"wxcq\" of Group ID and Code has already been taken."
}
{
"code": 20004,
"message": "数据验证失败:组合租户ID and 栏目代码的值\"\"015ce30b116ce86058fa6ab4fea4ac63\"-\"wxcq\"\"已经被占用了。"
}


