|
|
@@ -3,7 +3,10 @@
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
use App\ValueObjects\Cpf;
|
|
|
+use Carbon\Carbon;
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
+use Throwable;
|
|
|
|
|
|
class StudentRequest extends FormRequest
|
|
|
{
|
|
|
@@ -27,31 +30,52 @@ public function rules(): array
|
|
|
'how_did_you_know_us' => 'sometimes|nullable|string|in:referral,social_media,google,other',
|
|
|
'notes' => 'sometimes|nullable|string',
|
|
|
|
|
|
- 'responsible' => 'sometimes|nullable|array',
|
|
|
- 'responsible.name' => 'sometimes|nullable|string|max:255',
|
|
|
- 'responsible.birth_date' => 'sometimes|nullable|date',
|
|
|
- 'responsible.cpf' => ['sometimes', 'nullable', 'string', 'max:20', Cpf::rule()],
|
|
|
+ 'responsible' => [
|
|
|
+ 'nullable',
|
|
|
+ 'array',
|
|
|
+ Rule::requiredIf(fn (): bool => $this->studentIsMinor()),
|
|
|
+ ],
|
|
|
+
|
|
|
+ 'responsible.name' => 'required_with:responsible|string|max:255',
|
|
|
+ 'responsible.birth_date' => 'required_with:responsible|date',
|
|
|
+ 'responsible.cpf' => ['required_with:responsible', 'string', 'max:20', Cpf::rule()],
|
|
|
'responsible.gender' => 'sometimes|nullable|string|in:no_preference,male,female,other',
|
|
|
- 'responsible.degree' => 'sometimes|nullable|string|max:255',
|
|
|
- 'responsible.email' => 'sometimes|nullable|email',
|
|
|
- 'responsible.phone' => 'sometimes|nullable|string|max:20',
|
|
|
- 'responsible.postal_code' => 'sometimes|nullable|string|max:10',
|
|
|
- 'responsible.street' => 'sometimes|nullable|string|max:255',
|
|
|
+ 'responsible.degree' => 'required_with:responsible|string|max:255',
|
|
|
+ 'responsible.email' => 'required_with:responsible|email|max:255',
|
|
|
+ 'responsible.phone' => 'required_with:responsible|string|max:20',
|
|
|
+ 'responsible.postal_code' => 'required_with:responsible|string|max:10',
|
|
|
+ 'responsible.street' => 'required_with:responsible|string|max:255',
|
|
|
'responsible.address_number' => 'sometimes|nullable|string|max:20',
|
|
|
- 'responsible.neighborhood' => 'sometimes|nullable|string|max:255',
|
|
|
- 'responsible.city_id' => 'sometimes|nullable|integer|exists:cities,id',
|
|
|
- 'responsible.state_id' => 'sometimes|nullable|integer|exists:states,id',
|
|
|
+ 'responsible.neighborhood' => 'required_with:responsible|string|max:255',
|
|
|
+ 'responsible.city_id' => 'required_with:responsible|integer|exists:cities,id',
|
|
|
+ 'responsible.state_id' => 'required_with:responsible|integer|exists:states,id',
|
|
|
'responsible.complement' => 'sometimes|nullable|string|max:255',
|
|
|
'responsible.notes' => 'sometimes|nullable|string',
|
|
|
];
|
|
|
|
|
|
if ($this->isMethod('post')) {
|
|
|
- $rules['name'] = 'required|string|max:255';
|
|
|
- $rules['email'] = 'sometimes|nullable|email|unique:students,email';
|
|
|
+ $rules['name'] = 'required|string|max:255';
|
|
|
+ $rules['birth_date'] = 'required|date';
|
|
|
+ $rules['email'] = 'sometimes|nullable|email|unique:students,email';
|
|
|
} else {
|
|
|
$rules['name'] = 'sometimes|string|max:255';
|
|
|
}
|
|
|
|
|
|
return $rules;
|
|
|
}
|
|
|
+
|
|
|
+ private function studentIsMinor(): bool
|
|
|
+ {
|
|
|
+ $birthDate = $this->input('birth_date');
|
|
|
+
|
|
|
+ if (!is_string($birthDate) || $birthDate === '') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return Carbon::parse($birthDate)->age < 18;
|
|
|
+ } catch (Throwable) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|