美文网首页
Laravel 之 验证 validate 验证问题

Laravel 之 验证 validate 验证问题

作者: 小李不是你 | 来源:发表于2018-11-28 10:05 被阅读0次

    1. 表单提交验证

    use Illuminate\Support\Facades\Validator;
    $this->validate($request, [
                'goods_name' => 'required',
                'goods_type' => 'required',
                'goods_price' => 'required',
                'goods_info' => 'required',
            ], [
                'goods_name.required' => '商品名称不能为空',
                'goods_type.required' => '请选择商品分类',
                'goods_price.required' => '商品价格不能为空',
                'goods_info.required' => '商品信息不能为空',
            ]);
    
    validate文档链接: https://blog.csdn.net/cfun_goodmorning/article/details/78870631
    

    2. 使用ajax发送请求验证

    $data = $request->all();
    $rules = [
                'id'  => 'required | numeric',
                'status' => 'required',
            ];
    $message = [
                'id.required'  => 'id不能为空',
                'status.required' => '状态值不能为空',
            ];
    $validator = Validator::make($data,$rules,$message);
           
     if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return $this->returnJson(Status::FAIL, $msg[0], []);   
     }
          
    

    相关文章

      网友评论

          本文标题:Laravel 之 验证 validate 验证问题

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