PHP 表單提交後臺資料驗證 Validator

garbage_code發表於2020-10-31
public function added(Request $request){
        $data['author'] = $request->input('author');
        $data['sex'] = $request->input('sex');
        $data['age'] = $request->input('age');
        $data['phone'] = $request->input('phone');
        $data['email'] = $request->input('email');
        $data['content'] = $request->input('content');
        $val['author']= 'required'; //驗證必填
        $val['phone'] = 'unique:articles'; //驗證唯一
        $val['email'] = 'unique:articles';  //驗證唯一
            $msg = [
                'author.required'=>'作者不能為空',
                'phone.unique'=> '手機號已註冊',
                'email.unique'=>'郵箱重複了'
            ];
            $va = \Validator::make($data,$val,$msg);
            if($va->fails()){
                return $va->errors()->first();
            }
        $res = \DB::table('Articles')->insert($data);

相關文章