Laravel 5独特验证不起作用

时间:2015-05-26 09:12:55

标签: php validation laravel

我是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');
        }

    }

}

并且表单仍在提交?可能是什么问题呢?似乎验证不起作用?

表单应该会出错吗?但它仍然插入数据库

4 个答案:

答案 0 :(得分:4)

$rules = array(
    'firstname' => 'required|unique:users',
    'surname'   => 'required|unique:users',
);

了解更多:

答案 1 :(得分:2)

好像你在这里犯了错误

'required:unique'

应该是

'required|unique'

答案 2 :(得分:0)

  

解决方案:确保以双qoute形式进行写入验证,例如:" required"

 $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);

答案 3 :(得分:0)

我在Laravel 5.8中遇到了这个问题。我已经通过这种方式解决了。

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

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

This article帮助了我。