feat: add form request validation classes
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class SettingUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* آمادهسازی دادهها بر اساس ردیفهای موجود در جدول.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$allowedCodes = Setting::pluck('code')->all();
|
||||
|
||||
// فقط codeهایی که در جدول وجود دارند را نگاه میداریم
|
||||
$filtered = Arr::only($this->all(), $allowedCodes);
|
||||
|
||||
$this->merge($filtered);
|
||||
}
|
||||
|
||||
/**
|
||||
* قوانین اعتبارسنجی.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$codes = Setting::pluck('code')->all();
|
||||
|
||||
return collect($codes)
|
||||
->mapWithKeys(fn($code) => [
|
||||
$code => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:65535'
|
||||
],
|
||||
])
|
||||
->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user