DynamicCreateRequest.php 1.79 KB
<?php

namespace App\Http\Container\AppSection\Requests;

use Hikoon\LaravelApi\Support\ApiRequest;

class DynamicCreateRequest extends ApiRequest
{
    protected function prepareForValidation(): void
    {
        $this->mergeIfMissing([
            'intro' => ''
        ]);
    }

    /**
     * @return string[]
     */
    public function rules(): array
    {
        return [
            'type'                    => 'required|in:image,audio,video',
            'intro'                   => 'sometimes|nullable|string|max:400',
            'properties'              => 'required|array',
            'properties.url'          => 'required|string|url',
            'properties.width'        => 'sometimes|integer',
            'properties.height'       => 'sometimes|integer',
            'properties.duration'     => 'sometimes|numeric',
            'properties.cover'        => 'required_if:type,audio,video',
            'properties.cover.url'    => 'required_if:type,audio,video|string|url',
            'properties.cover.width'  => 'required_if:type,audio,video|integer',
            'properties.cover.height' => 'required_if:type,audio,video|integer',
        ];
    }

    /**
     * @return string[]
     */
    public function messages(): array
    {
        return [
            'type.required'                => '请选择创建方式',
            'type.in'                      => '创建方式暂未开放',
            'intro.max'                    => '描述内容长度不能超过:max字节',
            'properties.url.required'      => '请选择上传文件',
            'properties.url.url'           => '上传文件链接不正确',
            'properties.cover.required_if' => '请选择封面图片',
            'properties.cover.url'         => '封面图片链接不正确',
        ];
    }

}