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], []);
}
网友评论