Laravel验证:检查验证程序失败的原因 [英] Laravel Validation: check why validator failed

查看:121
本文介绍了Laravel验证:检查验证程序失败的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以检查验证器是否专门由于unique规则而失败?

If there a way to check whether or not the validator failed specifically because of the unique rule?

$rules = array(
            'email_address' => 'required|email|unique:users,email',
            'postal_code' => 'required|alpha_num',
        );

        $messages = array(
            'required' => 'The :attribute field is required',
            'email' => 'The :attribute field is required',
            'alpha_num' => 'The :attribute field must only be letters and numbers (no spaces)'
        );

        $validator = Validator::make(Input::all(), $rules, $messages);

        if ($validator->fails()) {

用通俗易懂的话来说,我基本上想知道:验证是否因为电子邮件地址不是唯一的而失败了?"

In laymans terms, I basically want to know: "did the validation fail because the email_address was not unique?"

推荐答案

在返回的失败规则数组中检查特定规则

Check for a specific rule within the returned array of failed rules

if ($validator->fails()) {

    $failedRules = $validator->failed();

    if(isset($failedRules['email_address']['Unique'])) {

    ...

这篇关于Laravel验证:检查验证程序失败的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆