php Laravel请求使用OOP多态性进行验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel请求使用OOP多态性进行验证相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Http\Requests\V1\VendorApplicationService;

use Dingo\Api\Http\FormRequest;

class StoreVendorApplicationServiceRequest 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()
    {
        $validate = [
            'appservice_name'=>'required|unique:application_services,appservice_name',
            'component_slug'=>'required|in:link,list,landing_page,detail',
            'description'=>'sometimes|required',
            'category_slug'=>'required',
            'tags'=>'array|required',
//            'categories'=>'array|required',
        ];

        $component_slug = request()->component_slug;

        if ($component_slug == 'list') {

            $validate = $validate + [
                'api_url'=>'required|url',
                'api_http_method'=>'required|in:POST,GET',
                'api_visible_fields'=>'required|array',
                'api_visible_fields.*'=>'required',
                'api_action_url'=>'required',
                'api_identifier_label'=>'required',
            ];
        }

        if ($component_slug == 'link') {

            $validate = $validate + [
                    'link_url'=>'required|url',
                ];

        }

        if ($component_slug == 'landing_page') {

            $validate = $validate + [
                    'landing_html'=>'required',
                ];

        }

        if ($component_slug == 'detail') {

            $validate = $validate + [
                    'detail_json'=>'required',
                ];

        }

        return $validate;
    }
}
<?php

namespace App\Http\Requests\V1\VendorApplicationService;

use App\Classes\ComponentValidator;
use App\Classes\ComponentValidatorFactory;
use Dingo\Api\Http\FormRequest;

class StoreVendorApplicationServiceRequest 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()
    {
        $validate = [
            'appservice_name'=>'required|unique:application_services,appservice_name',
            'component_slug'=>'required|in:link,list,landing_page,detail',
            'description'=>'sometimes|required',
            'category_slug'=>'required',
            'tags'=>'array|required',
//            'categories'=>'array|required',
        ];

        $component_validator = new ComponentValidator();

        $component = ComponentValidatorFactory::getComponent();

        $component_validator_rules = $component_validator->rules($component);

        $validate = $validate + $component_validator_rules;

        return $validate;
    }
}

以上是关于php Laravel请求使用OOP多态性进行验证的主要内容,如果未能解决你的问题,请参考以下文章

面向对象基础

使用oop php进行表单验证

OOP 三大特点:继承性,封装性,多态性

Laravel 4 多用户身份验证

如何在 PHP (Laravel) 中验证请求的源域

Laravel 5表单请求验证返回禁止错误