CustomRequest.php 2.1 KB
<?php

namespace App\Http\Requests\Bside\Custom;

use Illuminate\Foundation\Http\FormRequest;

/**
 * @author:dc
 * @time 2023/5/12 9:45
 * Class CustomRequest
 * @package App\Http\Requests\Bside\Custom
 */
class CustomRequest 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()
    {
        $rule = [
            'name'   =>  ['required','max:100'],
            'title'   =>  ['required','max:200'],
            'keywords'   =>  ['required','max:200'],
            'description'   =>  ['required','max:250'],
//            'html'   =>  ['required'],
            'url'   =>  ['required','max:200','url'],
            'status'   =>  ['required','in:0,1'],
        ];

        // 修改
        if($this->is('b/custom/update')){
            $rule['id'] = ['required','integer'];
        }

        // 删除
        if($this->is('b/custom/delete')){
            $rule = ['id' => ['required','integer']];
        }

        return $rule;
    }

    public function messages()
    {
        return [
            'id.required'  =>  '数据不存在',
            'id.integer'  =>  '数据不存在',

            'name.required'  =>  '名称必须',
            'name.max'  =>  '名称不能超过100个字符',
            'title.required'  =>  '网页标题必须',
            'title.max'  =>  '网页标题不能超过200个字符',
            'keywords.required'  =>  '网页关键字必须',
            'keywords.max'  =>  '网页关键字不能超过200个字符',
            'description.required'  =>  '网页描述必须',
            'description.max'  =>  '网页描述不能超过250个字符',

            'url.required'  =>  '链接必须',
            'url.max'  =>  '链接不能超过200个字符',
            'url.url'  =>  '链接不符合规则',

            'status.required'  =>  '状态选择错误',
            'status.in'  =>  '状态必须是显示/隐藏'
        ];
    }
}