code学习

Laravel 的Validation验证规则

Laravel版本是5.1LTS版本:

validation验证规则实例:

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      

json信息:

参考:

​​https://stackoverflow.com/questions/19422259/laravel-4-how-to-return-all-validation-error-messages-for-all-fields-as-a-json​​

Laravel 的Validation验证规则
<?php

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',
]);