Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from lucas0headshot/dev
Browse files Browse the repository at this point in the history
fix: issue #5
  • Loading branch information
lucas0headshot authored May 20, 2024
2 parents 48a5633 + ed12340 commit f1c6e88
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/Http/Requests/CompromissoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Rules\ValidInterval;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -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
],
];
}
Expand Down
36 changes: 36 additions & 0 deletions app/Rules/ValidInterval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ValidInterval implements ValidationRule
{
private $hora_inicio;
private $hora_fim;


public function __construct(string $hora_inicio, string $hora_fim)
{
$this->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.');
}
}
}
6 changes: 3 additions & 3 deletions resources/views/compromissos/create_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@

<div class="form-group mt-2">
<label for="hora_inicio" class="form-label">Hora de Início</label>
<input type="time" class="form-control @error('hora_inicio') is-invalid @enderror" id="hora_inicio" name="hora_inicio" value="{{ old('hora_inicio', isset($compromisso) ? $compromisso->hora_inicio : '') }}" required>
<input type="time" class="form-control @error('hora_inicio') is-invalid @enderror" id="hora_inicio" name="hora_inicio" value="{{ old('hora_inicio', isset($compromisso) ? substr($compromisso->hora_inicio, 0, 5) : '') }}" required>
@error('hora_inicio')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>

<div class="form-group mt-2">
<label for="hora_fim" class="form-label">Hora de Fim</label>
<input type="time" class="form-control @error('hora_fim') is-invalid @enderror" id="hora_fim" name="hora_fim" value="{{ old('hora_fim', isset($compromisso) ? $compromisso->hora_fim : '') }}" required>
<input type="time" class="form-control @error('hora_fim') is-invalid @enderror" id="hora_fim" name="hora_fim" value="{{ old('hora_fim', isset($compromisso) ? substr($compromisso->hora_fim, 0, 5) : '') }}" required>
@error('hora_fim')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>

<div class="form-group mt-2">
<label for="intervalo" class="form-label">Intervalo</label>
<input type="time" class="form-control @error('intervalo') is-invalid @enderror" id="intervalo" name="intervalo" value="{{ old('intervalo', isset($compromisso) ? $compromisso->intervalo : '') }}" required>
<input type="time" class="form-control @error('intervalo') is-invalid @enderror" id="intervalo" name="intervalo" value="{{ old('intervalo', isset($compromisso) ? substr($compromisso->intervalo, 0, 5) : '') }}" required>
@error('intervalo')
<p class="text-danger">{{ $message }}</p>
@enderror
Expand Down

0 comments on commit f1c6e88

Please sign in to comment.