Laravel 5 唯一验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30454318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:38:06  来源:igfitidea点击:

Laravel 5 Unique Validation Not Working

phpvalidationlaravel

提问by Hymanhammer013

I'm new to Laravel 5, supposed I have firstname and surname field on my database. I have this validation?

我是 Laravel 5 的新手,假设我的数据库中有名字和姓氏字段。我有这个验证?

public function create()
{

    $rules = array(
        'firstname'         => 'required:unique',
        'surname'           => 'required:unique',
        'phoneno'           => 'required',
        'mcmillan'          => 'required',
        'hri'               => 'required',
        'diabetes'          => 'required',
        'breastcancer'      => 'required',
        'gpt'               => 'required',
    );

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

    // Check if all fields is filled
    if ($validator->fails()) 
    {
        return Redirect::to('brk')->withErrors($validator);
    }
    else
    {
        $flag = Input::get('flag');

        /* If version 2 (Opt In) */
        if($flag == "1")
        {
            if(Input::get('opt') != "")
            {
                $optMethod = Input::get('opt');
            }
            else
            {
                $optMethod = 'OptOut';
            }
        }
        else if($flag == "0") /* If version 1 (Opt Out) */
        {
            if(Input::get('opt') != "")
            {
                $optMethod = Input::get('opt');
            }
            else
            {
                $optMethod = 'OptIn';
            }
        }

        $firstname      = Input::get('firstname');
        $surname        = Input::get('surname');
        $phoneno        = Input::get('phoneno');
        $mcmillan       = Input::get('mcmillan');
        $hri            = Input::get('hri');
        $diabetes       = Input::get('diabetes');
        $breastcancer   = Input::get('breastcancer');
        $gpt            = Input::get('gpt');

        $brk = new \App\Http\Models\BrkDailyMail;
        $brk->firstname            = $firstname;
        $brk->surname              = $surname;
        $brk->phoneno              = $phoneno;
        $brk->mcmillan             = $mcmillan;
        $brk->hri                  = $hri;
        $brk->diabetes             = $diabetes;
        $brk->breastcancer         = $breastcancer;
        $brk->gpt                  = $gpt;
        $brk->OptMethod             = $optMethod;
        $brk->hostname             = Request::root().'/brk';
        $brk->save();
        Session::flash('alert-success', 'Form Submitted Successfully.');
        /* If version 2 (Opt In) */
        if($flag == "1")
        {
            return Redirect::to('brk2');
        }
        else if($flag == "0") /* If version 1 (Opt Out) */
        {
            return Redirect::to('brk');
        }

    }

}

and the form is still submitting? what could be the problem? seems the validation won't work?

表格还在提交?可能是什么问题呢?似乎验证不起作用?

The form should make some errors right? but it still inserting in the database

表格应该有一些错误吧?但它仍然插入数据库中

回答by Limon Monte

回答by Christos Weip

Seems like you have a mistake here

好像你这里有错误

'required:unique'

Should be

应该

'required|unique'

回答by Kundan roy

Solutions : make sure write validation in double qoute like : "required"

解决方案:确保在双 qoute 中写入验证,例如:“必需”

 $validator = Validator::make($request->all(), [
                    'client_id'     =>  "required",
                    'chapter_name'   =>  "required|unique:chapters",
                    ]
                );
               /** Return Error Message **/
        if ($validator->fails()) {
                    $error_msg  =   [];
                                foreach ( $validator->messages()->messages() as $key => $value) {
                      $error_msg[$key] =   $value[0];
                    } 
        }  print_r($error_msg);

回答by Nole

I had this problem with Laravel 5.8. I have solved it this way.

我在 Laravel 5.8 上遇到了这个问题。我是这样解决的。

$validator = Validator::make($request->all(), [
                    'username'     =>  "required|unique:users,username",
                    'password'   =>  "required",
                    ]
                );

Before this I got errors in validation from Laravel. When I did validation I have used unique:users. This way Laravel was not sure what column should be unique in table users. I should add unique:users,usernameto specify unique column username in user table.

在此之前,我在 Laravel 的验证中遇到错误。当我进行验证时,我使用了unique:users。这样 Laravel 不确定表用户中哪个列应该是唯一的。我应该添加 unique:users,username以在用户表中指定唯一的列用户名。

This articlehelped me.

这篇文章帮助了我。