71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class ClientCategoryRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$categoryId = $this->route('client_category')?->id;
|
|
|
|
return [
|
|
'name' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
Rule::unique('client_categories')->ignore($categoryId)
|
|
],
|
|
'slug' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
'alpha_dash',
|
|
Rule::unique('client_categories')->ignore($categoryId)
|
|
],
|
|
'description' => 'nullable|string|max:1000',
|
|
'is_active' => 'boolean',
|
|
'sort_order' => 'nullable|integer|min:0',
|
|
];
|
|
}
|
|
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => 'category name',
|
|
'slug' => 'URL slug',
|
|
];
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
// Generate slug from name if not provided and name exists
|
|
if (!$this->slug && $this->name) {
|
|
$this->merge([
|
|
'slug' => \Str::slug($this->name),
|
|
]);
|
|
}
|
|
|
|
// Ensure boolean values are properly cast
|
|
if ($this->has('is_active')) {
|
|
$this->merge([
|
|
'is_active' => filter_var($this->is_active, FILTER_VALIDATE_BOOLEAN),
|
|
]);
|
|
}
|
|
}
|
|
}
|