From ed1234008b28e51e7423a4f6d30742a4e360d99a Mon Sep 17 00:00:00 2001 From: lucas0headshot Date: Sun, 19 May 2024 23:13:57 -0300 Subject: [PATCH] fix: issue #5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adicionar regra de validação pro intervalo. Adicionar remoção dos segundos dos campos hora_inicio e hora_fim, ao executar a action edit. --- app/Http/Requests/CompromissoRequest.php | 7 +++- app/Rules/ValidInterval.php | 36 +++++++++++++++++++ .../views/compromissos/create_edit.blade.php | 6 ++-- 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 app/Rules/ValidInterval.php diff --git a/app/Http/Requests/CompromissoRequest.php b/app/Http/Requests/CompromissoRequest.php index 66e0956..f96e505 100644 --- a/app/Http/Requests/CompromissoRequest.php +++ b/app/Http/Requests/CompromissoRequest.php @@ -2,6 +2,7 @@ namespace App\Http\Requests; +use App\Rules\ValidInterval; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; @@ -37,7 +38,11 @@ public function rules(): array 'data' => 'required|date|date_format:Y-m-d', 'hora_inicio' => 'required|date_format:H:i', 'hora_fim' => 'required|after:hora_inicio|date_format:H:i', - 'intervalo' => 'required|date_format:H:i', + 'intervalo' => [ + 'required', + 'date_format:H:i', + new ValidInterval($this->input('hora_inicio'), $this->input('hora_fim')) //*Issue #5 - https://github.com/lucas0headshot/teste-tecnico_exon_2024/issues/5 + ], ]; } diff --git a/app/Rules/ValidInterval.php b/app/Rules/ValidInterval.php new file mode 100644 index 0000000..71adab0 --- /dev/null +++ b/app/Rules/ValidInterval.php @@ -0,0 +1,36 @@ +hora_inicio = $hora_inicio; + $this->hora_fim = $hora_fim; + } + + + /** + * Run the validation rule. + * + * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + */ + public function validate(string $attribute, mixed $value, Closure $fail): void + { + $hora_inicio = strtotime($this->hora_inicio); + $hora_fim = strtotime($this->hora_fim); + $intervalo = strtotime($value) - strtotime('TODAY'); + + if (($hora_fim - $hora_inicio) <= $intervalo) { + $fail('O intervalo não pode ser maior ou igual ao período entre a hora de início e hora de fim.'); + } + } +} diff --git a/resources/views/compromissos/create_edit.blade.php b/resources/views/compromissos/create_edit.blade.php index d77c443..0cae2d6 100644 --- a/resources/views/compromissos/create_edit.blade.php +++ b/resources/views/compromissos/create_edit.blade.php @@ -39,7 +39,7 @@
- + @error('hora_inicio')

{{ $message }}

@enderror @@ -47,7 +47,7 @@
- + @error('hora_fim')

{{ $message }}

@enderror @@ -55,7 +55,7 @@
- + @error('intervalo')

{{ $message }}

@enderror