Laravel版本是5.1LTS版本:
public function saveOrganizationCode(Request $request) {
$this->validate($request, [
'organization_name' => 'required',
'organization_id' => 'required|int',
'auth_num' => 'required|int',
'resource' => 'required',
//experitime和experimonth只能二选一,并且experitime只能是大于明天的时间
'experitime' => 'date|after:tomorrow|required_without:experimonth',
'experimonth' => 'int|required_without:experitime',
'active_num' => 'required|int',
'auth_codes' => 'required',
],[
'active_num.required' => '授权数是必须的!',
]);
}
https://laravel.com/docs/5.1/validation#rule-mimes
错误展示,所有模板通用,也可以把错误信息整合为json返回给前端接口.
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
https://stackoverflow.com/questions/19422259/laravel-4-how-to-return-all-validation-error-messages-for-all-fields-as-a-json
经过代码实践,我发现把验证规则集中在一起,比较好管理和修改,常用规则如下:
use Illuminate\Validation\Rule;
return [
'every_request_need_params' => [
'interface_type' => 'required|in:1,2',
'version' => 'required',
],
'login' => [
'area_phone_number' => 'required|numeric',
'telephone' => 'required|numeric',
'type' => 'required|in:1,2',
'password' => 'required_if:type,1|alpha_dash|min:6|max:32',
'auth_code' => 'required_if:type,2|digits:6',
],
'reset_password' => [
'area_phone_number' => 'required|numeric',
'telephone' => 'required|numeric',
'auth_code' => 'required|digits:6',
'password' => 'required|alpha_dash|min:6|max:32',
'confirm_password' => 'required|same:password',
],
'register' => [
'area_phone_number' => 'required|numeric',
'wechat' => ['required', 'regex:/^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}+$/'],
// 'child_birthday' => 'sometimes|date_format:Y-m-d|before:now',
'telephone' => ['required', 'numeric',
Rule::unique('customer')->where(function ($query) {
$query->where('area_phone_number', request('area_phone_number'))
->whereNotIn('status', [4]);
})],
'auth_code' => 'required|digits:6',
'password' => 'required|alpha_dash|min:6|max:32',
],
'sendMessage' => [
'area_phone_number' => 'required|numeric',
'telephone' => 'required|numeric',
'type' => 'required|in:1,2,3,4',
],
'checkAuthCode' => [
'area_phone_number' => 'required|numeric',
'telephone' => 'required|numeric',
'type' => 'required|in:1,2,3,4',
'auth_code' => 'required|numeric|digits:6',
],
'update_customer_info' => [
'customer_id' => 'required|integer',
'chinese_name' => 'required|string|min:2|max:32',
'english_name' => 'required|alpha_dash|min:2|max:32',
'wechat' => 'required|alpha_dash|min:2|max:32',
'email' => ['required', 'email',
Rule::unique('customer')->ignore(request('customer_id'))->where(function ($query) {
$query->whereNotIn('status', [4]);
})],
'country_id' => 'required|integer',
'province_id' => 'required|integer',
'city_id' => 'nullable|integer',
],
'update_student_info' => [
'student_info_id' => 'required|integer',
'birthday' => 'required|before:now',
'chinese_name' => 'required|string|min:2|max:32',
'english_name' => 'required|alpha_dash|min:2|max:32',
'gender' => 'required|in:1,2',
],
];
在使用正则验证的时候, 一定要使用数组来表示各个验证,而不是"|"
$data = request()->validate([
'name' => 'required|min:2|unique:channel_user,name,' . request('id'),
# 排除自己
'password' => [
'nullable',
'regex:/^[a-zA-Z]{1,}([a-zA-Z0-9]|[._#$*&]){4,19}$/',
],
'type' => 'required',
'email' => 'required|email|unique:channel_user,email,' . request('id'),
'title' => [
'required', 'string', 'between:3,64',
Rule::unique('channel_form')->where(function ($query) use ($channel_user_id) {
$query->where('channel_user_id', $channel_user_id);
}),
],
'wechat' => 'nullable|string|min:6',
]);