HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api_matriculas/app/Http/Requests/Student/StoreStudentRequest.php
<?php

namespace App\Http\Requests\Student;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rule;

class StoreStudentRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'document_type' => 'required|string|in:RUT,PASSPORT',
            'rut' => 'required|string|max:50',
            'first_name' => 'required|string|max:255',
            'second_name' => 'nullable|string|max:255',
            'last_name' => 'required|string|max:255',
            'second_last_name' => 'nullable|string|max:255',
            'email' => 'nullable|email|max:255',
            'mobile' => 'nullable|string|max:15',
            'birth_date' => 'nullable|date',
            'gender' => 'nullable|integer|exists:genders,id',
            'country' => 'nullable|integer|exists:countries,id',
            'region' => 'nullable|integer|exists:regions,id',
            'commune' => 'nullable|integer|exists:communes,id',
            'parent' => 'required|integer|exists:parents,id',
            'course' => 'required|integer|exists:courses,id',
            'course_letter' => 'nullable|integer|exists:courses_letters,id',
            'letter' => 'nullable|string|max:5',
            'date_entry' => 'nullable|date',
            'address' => 'nullable|string|max:255',
            'family'                 => 'required|boolean',
            'period'                 => 'required|integer|exists:periods,id',
            'pays_incorporation_fee' => 'required|boolean',
        ];
    }

    public function messages()
    {
        return [
            'document_type.required' => 'Tipo de documento requerido',
            'document_type.string' => 'Tipo de documento inválido',
            'document_type.in' => 'El tipo de documento debe ser RUT o PASAPORTE',
            'rut.required' => 'Documento de identificación requerido',
            'rut.string' => 'Documento de identificación inválido',
            'rut.max' => 'El documento no puede superar 50 caracteres',
            'first_name.required' => 'Primer nombre requerido',
            'first_name.string' => 'Primer nombre inválido',
            'first_name.max' => 'El primer nombre no puede superar 255 caracteres',
            'second_name.string' => 'Segundo nombre inválido',
            'second_name.max' => 'El segundo nombre no puede superar 255 caracteres',
            'last_name.required' => 'Apellido paterno requerido',
            'last_name.string' => 'Apellido paterno inválido',
            'last_name.max' => 'El apellido paterno no puede superar 255 caracteres',
            'second_last_name.string' => 'Apellido materno inválido',
            'second_last_name.max' => 'El apellido materno no puede superar 255 caracteres',
            'email.required' => 'Correo electrónico requerido',
            'email.email' => 'Formato de correo inválido',
            'email.max' => 'El correo electrónico no puede superar 255 caracteres',
            'mobile.string' => 'Teléfono móvil inválido',
            'mobile.max' => 'El teléfono móvil no puede superar 15 caracteres',
            'birth_date.date' => 'Formato de fecha inválido',
            'gender.integer' => 'Género inválido',
            'gender.exists' => 'Género no encontrado',
            'country.integer' => 'País inválido',
            'country.exists' => 'País no encontrado',
            'region.integer' => 'Región inválida',
            'region.exists' => 'Región no encontrada',
            'commune.integer' => 'Comuna inválida',
            'commune.exists' => 'Comuna no encontrada o no pertenece a la región seleccionada',
            'parent.integer' => 'Apoderado inválido',
            'parent.exists' => 'Apoderado no encontrado',
            'course.integer' => 'Curso inválido',
            'course.exists' => 'Curso no encontrado',
            'course_letter.integer' => 'Letra de curso inválida',
            'course_letter.exists' => 'Letra de curso no encontrada',
            'letter.string' => 'La letra es inválida',
            'letter.max' => 'La letra no puede superar 5 caracteres',
            'date_entry.date' => 'La fecha de ingreso es inválida',
            'address.string' => 'Dirección inválida',
            'address.max' => 'La dirección no puede superar 255 caracteres',
            'period.required' => 'El periodo es requerido',
            'period.integer' => 'El periodo es inválido',
            'period.exists' => 'El periodo no existe',
            'pays_incorporation_fee.boolean' => 'El valor de cuota de incorporación debe ser booleano',
        ];
    }

    public function withValidator(Validator $validator)
    {
        $validator->after(function ($validator) {
            if (($this->document_type ?? 'RUT') === 'RUT' && !empty($this->rut)) {
                if (!validateRut($this->rut)) {
                    $validator->errors()->add('rut', 'El RUT ingresado no es válido.');
                }
            }
        });
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(
            genericResponse(['error' => implode(', ', $validator->errors()->all())], 400)
            );
    }
}