55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class CategoryRequest 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, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$categoryId = $this->route('category')?->id;
|
|
$nameRule = $this->isMethod('post') ? 'required' : 'sometimes';
|
|
|
|
return [
|
|
'name' => [$nameRule, 'string', 'max:255'],
|
|
'slug' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
'alpha_dash:ascii',
|
|
Rule::unique('categories', 'slug')->ignore($categoryId),
|
|
],
|
|
'description' => ['nullable', 'string'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => 'nome',
|
|
'slug' => 'slug',
|
|
'description' => 'descrição',
|
|
];
|
|
}
|
|
}
|