$data = $request->input();
$rules = [
'id' => 'required|numeric',
'age' => 'required|numeric|max:150',
'name' => 'required|max:20', //sometimes的用意(不传则已,传则必须遵守规则)
];
$messages = [
'required' => ':attribute不能为空',
'numeric' => ':attribute必须是数字',
'max' => ':attribute长度(数值)不应该大于 :max',
];
$attributes = [
'id' => 'ID',
'age' => '年龄',
'name' => '名称',
];
$validator = \Validator::make($data, $rules, $messages, $attributes);
if ($validator->fails()) {
return $validator->errors()->all(); //显示所有错误组成的数组
//return $validator->errors()->first(); //显示第一条错误
} else {
return 'validate passed';
}