是否可以在laravel请求类中添加验证规则的名称和描述

时间:2017-01-19 10:11:48

标签: php laravel

我有请求类:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'        => 'required',
            'description' => 'required',
        ];
    }
}

但我想要类似的东西:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'validation' => 'required',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'validation' => 'required',
                'type' => 'string',
            ]
        ];
    }
}

因为我想将这些附加数据用于Rest API文档。我不想复制像这样的名字:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function apiData(){
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'type' => 'string',
            ]
        ];
    }

    public function rules()
    {
        return [
            'name'        => 'required',
            'description' => 'required',
        ];
    }
}

1 个答案:

答案 0 :(得分:2)

您可以rules使用apiData,如下所示:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function apiData()
    {
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'validation' => 'required',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'validation' => 'required',
                'type' => 'string',
            ]
        ];
    }

    public function rules()
    {
        return array_map(function($item){
            return $item['validation'];
        }, $this->apiData());
    }
}

但是,查看Swagger文档可能是个好主意。 larave的绑定很少:https://packagist.org/search/?q=swagger%20laravel

修改

rulesapiData的一种适配器,用于保存数据。因此,对于未提及的问题,您需要将您的逻辑置于apiData而不更改rules中的逻辑:

public function apiData()
{
    return array_diff_key(
        (new Requests\Backend\UpdateProductRequest)->apiData(),
        ['something' => false]
    );
}

public function rules()
{
    return array_map(function($item){
        return $item['validation'];
    }, $this->apiData());
}