laravel创建表单请求验证

臭大佬 2019-10-16 17:50:02 1413
php  laravel 
简介 创建表单请求验证

创建验证器

<?php

namespace App\Http\Requests\Api;

use App\Http\Requests\BaseRequest;
use App\Models\Hostpool;
use App\Models\Ssl;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Models\HostpoolDomainrules;

class SslRequest extends BaseRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];
        if ($this->isMethod('post')) {
            $rules = [
                'hostpool_id' => ['required', function ($attribute, $value, $fail) {
                    $info = Hostpool::find($value);
                    if (!$info) {
                        $fail('该实例不存在');
                        return false;
                    }
                    $this->merge(['user_id' => $info->user_id]);
                }],
                'name' => 'required',
                'crt' => 'required',
                'key' => 'required',
            ];
        } elseif ($this->isMethod('patch')) {
            $rules = [
                'id' => ['required'],
                'hostpool_id' => [function ($attribute, $value, $fail) {
                    $info = Hostpool::find($value);
                    if (!$info) {
                        $fail('该实例不存在');
                        return false;
                    }
                    $this->merge(['user_id' => $info->user_id]);
                }],
                'name' => 'required',
                'crt' => 'required',
                'key' => 'required',
            ];
        }
        return $rules;
    }

    /**
     * Description:
     * User: Vijay <1937832819@qq.com>
     * Date: 2019/11/26
     * Time: 13:58
     * @return array
     */
    public function messages()
    {
        return [
            'id.required' => '修改时,id必填',
            'name.required' => '证书名称必填',
            'crt.required' => '证书内容必填',
            'key.required' => '证书密钥必填',
            'hostpool_id.unique' => '所属实例ID不能重复',
        ];
    }

    /**
     * Description:
     * User: Vijay <1937832819@qq.com>
     * Date: 2019/11/26
     * Time: 13:58
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => 'ID',
            'hostpool_id' => '所属实例ID',
            'name' => '证书名称',
            'crt' => '证书内容',
            'key' => '证书密钥',
        ];
    }
}

 /**
     * Description:验证后操作
     * User: Vijay <1937832819@qq.com>
     * Date: 2020/01/02
     * Time: 17:29
     * @param \Illuminate\Contracts\Validation\Validator $validator
     */
    public function withValidator(\Illuminate\Contracts\Validation\Validator $validator)
    {
        $validator->after(function ($validator) {
            $query = HostpoolDomainrulesCache::query()
                ->where('hostpool_id', $this->hostpool_id);
            $isset = $query->first();
            if ($isset) {
                $validator->errors()
                    ->add('rules', '规则错误');
            }
        });
    }

父类

<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class BaseRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    /**
     * Description:返回错误相应
     * User: Vijay <1937832819@qq.com>
     * Date: 2019/11/19
     * Time: 16:43
     * @param string $msg
     * @param null $data
     */
    protected function getFailedResponse($msg = '', $data = null, $http_code = 200)
    {
        throw (new HttpResponseException(response()->json([
            'data' => $data,
            'status' => false,
            'message' => $msg,
            'meta' => null
        ], $http_code)));
    }

    /**
     * Description:返回错误信息
     * User: Vijay <1937832819@qq.com>
     * Date: 2019/11/19
     * Time: 16:24
     * @param Validator $validator
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        $this->getFailedResponse($validator->errors()->first());
    }
}

调用

 public function store(SslRequest $request)
 {
 //......
 }