Your IP : 216.73.216.79


Current Path : /var/www/html/reddsis/app/Http/Requests/Backend/
Upload File :
Current File : /var/www/html/reddsis/app/Http/Requests/Backend/RefRequest.php

<?php

namespace App\Http\Requests\Backend;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RefRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $id = $this->route('id'); // untuk unique ignore semasa update

        return [
            'cat'       => 'required|string|max:255',
            'code'      => [
                'required',
                'string',
                'max:255',
                Rule::unique('ref', 'code')->where('cat', $this->cat)->ignore($id),
            ],
            'descr'     => 'nullable|string|max:255',
            'descr_en'  => 'nullable|string|max:255',
            'sort'      => 'nullable|integer|min:0',
            'parent'    => 'nullable|string|max:255',
            'icon_name' => 'nullable|string|max:255',
        ];
    }

    /**
     * Get custom messages for validation errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'cat.required'  => 'Category is required.',
            'cat.max'       => 'Category may not be greater than 255 characters.',
            'code.required' => 'Code is required.',
            'code.unique'   => 'This code already exists under the same category.',
            'code.max'      => 'Code may not be greater than 255 characters.',
            'sort.integer'  => 'Sort order must be a number.',
            'sort.min'      => 'Sort order must be at least 0.',
        ];
    }
}