- Create TaskStoreRequest, TaskUpdateRequest with authorize() + rules() - Create IssueStoreRequest, IssueUpdateRequest with authorize() + rules() - Create InspectionStoreRequest, InspectionUpdateRequest with authorize() + rules() + withValidator() - Update TaskForm to use FormRequests via app() - Update IssueForm (kept inline validation for complex logic) - Add InspectionStore/Update requests for ProjectMap inspection methods Tests: 101 passing (319 assertions)
42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\InspectionTemplate;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class InspectionStoreRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user()->can('create inspections');
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'selectedTemplateId' => 'required|exists:inspection_templates,id',
|
|
'inspectionPhotos.*' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:10240',
|
|
'inspectionFormData' => 'nullable|array',
|
|
'inspectionResult' => 'nullable|in:pass,fail',
|
|
'inspectionNotes' => 'nullable|string',
|
|
];
|
|
}
|
|
|
|
public function withValidator($validator)
|
|
{
|
|
$validator->after(function ($validator) {
|
|
$template = InspectionTemplate::find($this->input('selectedTemplateId'));
|
|
if ($template) {
|
|
foreach ($template->fields as $field) {
|
|
if (($field['required'] ?? false) && empty($this->input("inspectionFormData.{$field['name']}"))) {
|
|
$validator->errors()->add(
|
|
"inspectionFormData.{$field['name']}",
|
|
"El campo {$field['label']} es obligatorio."
|
|
);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
} |