laravel学习——表单验证

// 前端页面:

{{ csrf_field() }}






//错误显示位置
@if ($errors->any())



    @foreach ( e r r o r s > a l l ( ) a s error)
  • {{ $error }}
  • @endforeach


@endif

//后端验证页面:
public function check(Request $request)
{

    //验证规则
    $validatedData = $request->validate([
        'tel' => 'required|regex:/^1[34578]\d{9}$/',
        'pwd' => 'required|min:6|max:16|confirmed',
        'check_pwd' => 'required|same:pwd',
        'check_code' => 'required',


    ], [
    //错误提示信息
        'tel.required' => '手机号不能为空',
        'tel.regex' => '请输入有效的手机号',
        'pwd.min' => '密码不能少于6位',
        'pwd.max' => '密码不能超过16位',
        'pwd.required' => '密码不能为空',
        'pwd.confirmed' => '密码与确认密码不一致',
        'check_pwd.same' => '密码与确认密码不一致',
        'check_pwd.required' => '请输入确认密码',
        'check_code.required' => '请填入手机验证码',
    ]);

}

方法2:
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
public function welcome(Request request){ valid = Validator::make( r e q u e s t > a l l ( ) , [ n a m e => r e q u i r e d | s t r i n g | m i n : 2 , u s e r => r e q u i r e d , ] ) ; i f ( valid->fails()) {
return array(
‘code’ => ExceptionErrorCode::CODEMUST,
‘massage’ => v a l i d > e r r o r s ( ) > f i r s t ( ) , d a t a => valid->errors()->first()
);
}

    $id = $request->id;
    $res = User::where('id',$id)->get(['username','money','integral']);


    var_dump($res);
}

//附:条件验证规则地址http://laravelacademy.org/post/7978.html#toc_17

猜你喜欢

转载自blog.csdn.net/weixin_39815001/article/details/82492115