ProductRequest.php 1.6 KB
<?php

namespace App\Http\Requests\Bside\Product;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Models\Product\Product;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

/**
 * Class ProductRequest
 * @package App\Http\Requests\Bside\product
 * @author zbj
 * @date 2023/4/12
 */
class ProductRequest 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()
    {
        return [
            'title' => 'required|max:200',
            'route' => 'required|max:200',
            'seo_mate.keywords' => 'max:300',
            'seo_mate.description' => 'max:200',
            'status' => ['required', Rule::in(array_keys(Product::statusMap()))],
        ];
    }

    public function messages()
    {
        return [
            'title.required' => '请输入产品标题',
            'title.max' => '产品标题不能超过200个字符',
            'route.required' => '请输入产品链接',
            'route.max' => '产品链接不能超过200个字符',
            'status.required' => '请选择产品状态',
            'status.in' => '产品状态值异常',
            // 可选的 seo_mate 子字段的提示
            'seo_mate.keywords.max'     => 'SEO 关键字不能超过200个字符',
            'seo_mate.description.max' => 'SEO 描述不能超过200个字符',
        ];
    }

}