BannerRequest.php 2.43 KB
<?php

namespace App\Http\Container\AdminSection\Requests\System;

use Closure;
use Hikoon\LaravelApi\Support\ApiRequest;

class BannerRequest extends ApiRequest
{

    public function prepareForValidation(): void
    {
        $this->mergeIfMissing(['content_picture' => '', 'content' => [], 'permission' => []]);
    }

    public function rules(): array
    {
        return [
            'name'            => 'required|string|max:200',
            'scope'           => 'required|numeric|in:1',
            'weight'          => 'required|numeric|between:0,200',
            'permission'      => 'sometimes|array',
            'permission.*'    => 'string',
            'type'            => 'required|numeric|in:1,2,3,4',
            'cover'           => 'required|url',
            'content_picture' => "required_if:type,2|url",
            'content'         => ['required_if:type,3,4', function (string $attribute, mixed $value, Closure $fail) {
                if ($this->integer('type') === 3 && !is_array($value)) {
                    $fail('详情类型错误');
                }
                if ($this->integer('type') === 4 && !is_string($value)) {
                    $fail('详情类型错误');
                }
            }]
        ];
    }

    public function messages(): array
    {
        return [
            'name.required'               => '请输入名称',
            'name.max'                    => '名称长度不能超过:max',
            'scope.required'              => '请选择展示位置',
            'scope.in'                    => '展示位置不在指定范围内',
            'weight.required'             => '请输入权重值',
            'weight.numeric'              => '权重值类型错误',
            'weight.between'              => '权重值不在指定范围内',
            'role.array'                  => '浏览用户类型错误',
            'type.required'               => '请选择类型',
            'type.in'                     => '类型不在指定范围内',
            'cover.required'              => '请上传封面',
            'cover.url'                   => '无效的封面链接地址',
            'content_picture.required_if' => '请输入详情链接',
            'content_picture.url'         => '无效的详情链接地址',
            'content.required_if'         => '详情内容不能为空',
            'content.array'               => '详情类型错误'
        ];
    }
}