Php Yii2在错误消息中显示名称而不是id(客户id)

Php Yii2在错误消息中显示名称而不是id(客户id),php,yii2,Php,Yii2,我想在错误消息中显示客户名称而不是客户id 如何做到这一点 下面是我的模型中的关系 public function getCustomer() { return $this->hasOne(Customer::className(), ["id" => "customer_id"]); } 模型验证规则,在错误消息中,我必须设置客户名称 public function rules() { return [

我想在错误消息中显示客户名称而不是客户id

如何做到这一点

下面是我的模型中的关系

public function getCustomer() {
    return $this->hasOne(Customer::className(), ["id" => "customer_id"]);
}
模型验证规则,在错误消息中,我必须设置客户名称

public function rules()
        {
            return [         
                ['customer_id','unique','message'=>'Customer **{here i have to set customer name}** already exist in rd pool'],

            ];
        }

使用服装验证进行此提议

models类的first-in-rule函数声明验证函数

return [

        ['id', 'validateID', 'when'=> function($model){
                return (!empty($model->id))? true:false;
            },],
        ......
];
然后在同一个模型中创建名为validateId的公共函数

public function validateId($attribute, $params, $validator)
{
    $id = $this->id;

   { here you can write code to check}
    if(condition check)){
        $this->addError('id', "Coustumer $userName booking start date");
        return false;
    }
}
然后在表单中添加'enableAjaxValidation'=>True

 <?php $form = ActiveForm::begin([
    'enableAjaxValidation' => true
    ]); ?>
如果您有一些困惑,请参考


显示相关代码..假设:您的关系名称
customer
。尝试客户
$this->Customer->name
已被采用。@Inaseskull你说得对,我们可以通过这种方式获取客户名称,但当我在错误消息中尝试此方法时,我没有获取客户名称。请检查,我已经添加了我的验证规则。是的,我可以创建自定义验证程序,但我认为这是常见的要求,所以我已经取消了代码。但我不知道我该怎么做
if($request->isAjax && $model->load($request->post())){
        yii::$app->response->format = 'json';
        return ActiveForm::validate($model);
    }