美文网首页
larvel Validator

larvel Validator

作者: 云窗96 | 来源:发表于2018-08-08 23:19 被阅读0次
    Validator验证
    控制器验证
    // ValidatesRequests trait的validate方法
    $this->validator($request,
                   [
                       'student.username'=>'required|min:2|max:20',
                       'student.mobile'=>'required|digits:11',
                       'student.sex'=>'required|integer',
                   ],['required'=>':attribute为必填项',
                       'min'=>':attribute长度不合法',
                       'integer'=>':attribute长度不合法啊!'
                   ],['student.username'=>'用户名',
                       'student.mobile'=>'手机号',
                       'student.sex'=>'性别']
    );
    
    // 报错时自动返回错误页面有$errors全局变量
        $errors->all()
    
    Validator门面
     $validator=Validator::make($request->input(),
                    [
                        'student.username'=>'required|min:2|max:20|unique:student',
                        'student.mobile'=>'required',
                        'student.sex'=>'required|integer',
                    ],['required'=>':attribute为必填项',
                        'min'=>':attribute长度不合法',
                        'integer'=>':attribute长度不合法啊!'
                    ],['student.username'=>'用户名',
                        'student.mobile'=>'手机号',
                        'student.sex'=>'性别']
                );
                if($validator->fails()){
                    return redirect()->back()->withErrors($validator)->withInput();
                }
    
    // 第一条报错 消息
                $errors=$validator->errors();
                $errors->first(‘student.username’);
    //返回输入
        value="{{ old('student')['username'] ? old('student')['username'] : $student->username }}"
    

    相关文章

      网友评论

          本文标题:larvel Validator

          本文链接:https://www.haomeiwen.com/subject/frpfbftx.html