NotificationRequest.php 2.29 KB
<?php

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

use Hikoon\LaravelApi\Support\ApiRequest;
use Illuminate\Support\Carbon;

class NotificationRequest extends ApiRequest
{

    protected function prepareForValidation(): void
    {
        $this->mergeIfMissing(['rich_content' => '', 'publish_at' => now()->toDateTimeString()]);
        $this->merge(['publish_at' => Carbon::parse($this->input('publish_at'))->toDateTimeString()]);
    }


    public function rules(): array
    {
        return [
            'title'        => 'required|string|max:30',
            'type'         => 'required|in:1,2',
            'cover'        => 'required_if:type,2',
            'content'      => 'required|string|max:200',
            'link_type'    => 'required|in:none,user,activity,project,rich',
            'link_id'      => 'required_if:link_type,user,activity,project|numeric',
            'rich_content' => 'required_if:link_type,rich',
            'publish_type' => 'required|in:1,2',
            'publish_at'   => 'required_if:publish_type,2|date|after_or_equal:now',
            'publish_to'   => 'required|array',
            'is_alert'     => 'required|in:0,1'
        ];
    }

    public function messages(): array
    {
        return [
            'title.required'            => '请输入标题',
            'title.max'                 => '标题长度不能超过:max字节',
            'content.required'          => '请输入内容',
            'content.max'               => '内容长度不能超过:max字节',
            'type.required'             => '请选择类型',
            'type.in'                   => '类型不在指定范围内',
            'cover.required_if'         => '请上传封面图',
            'link_type.required'        => '请选择交互方式',
            'link_type.in'              => '交互方式未在指定范围内',
            'link_id.required_if'       => '请选择交互对象',
            'rich_content.required_if'  => '请输入文本内容',
            'publish_type.required'     => '请选择发布方式',
            'publish_type.in'           => '发布方式不在指定范围内',
            'publish_at.after_or_equal' => '发布时间不能早上当前时间',
            'is_alert.required'         => '请选择是否弹窗'
        ];
    }
}