Commit fc5d2144 authored by Guilherme Silva's avatar Guilherme Silva
Browse files

Validação do cpf na substituição

parent 34ccb008
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use PDF; use PDF;
use App;
use Auth; use Auth;
use App\Area; use App\Area;
use App\User; use App\User;
...@@ -1452,6 +1453,7 @@ class TrabalhoController extends Controller ...@@ -1452,6 +1453,7 @@ class TrabalhoController extends Controller
$substituicao = new Substituicao(); $substituicao = new Substituicao();
$substituicao->observacao = $request->textObservacao; $substituicao->observacao = $request->textObservacao;
\App\Validator\CpfValidator::validate ($request->all());
$user = User::where('email' , $data['email'])->first(); $user = User::where('email' , $data['email'])->first();
if (!$user){ if (!$user){
$data['usuarioTemp'] = true; $data['usuarioTemp'] = true;
...@@ -1529,6 +1531,9 @@ class TrabalhoController extends Controller ...@@ -1529,6 +1531,9 @@ class TrabalhoController extends Controller
Mail::to($evento->coordenadorComissao->user->email)->send(new SolicitacaoSubstituicao($evento, $trabalho)); Mail::to($evento->coordenadorComissao->user->email)->send(new SolicitacaoSubstituicao($evento, $trabalho));
return redirect(route('trabalho.trocaParticipante', ['evento_id' => $evento->id, 'projeto_id' => $trabalho->id]))->with(['sucesso' => 'Pedido de substituição enviado com sucesso!']); return redirect(route('trabalho.trocaParticipante', ['evento_id' => $evento->id, 'projeto_id' => $trabalho->id]))->with(['sucesso' => 'Pedido de substituição enviado com sucesso!']);
}catch (\App\Validator\ValidationException $th){
DB::rollback();
return redirect(route('trabalho.trocaParticipante', ['evento_id' => $evento->id, 'projeto_id' => $trabalho->id]))->with(['erro' => "Cpf inválido"]);
}catch (\Throwable $th) { }catch (\Throwable $th) {
DB::rollback(); DB::rollback();
return redirect(route('trabalho.trocaParticipante', ['evento_id' => $evento->id, 'projeto_id' => $trabalho->id]))->with(['erro' => $th->getMessage()]); return redirect(route('trabalho.trocaParticipante', ['evento_id' => $evento->id, 'projeto_id' => $trabalho->id]))->with(['erro' => $th->getMessage()]);
......
...@@ -13,6 +13,8 @@ class User extends Authenticatable implements MustVerifyEmail ...@@ -13,6 +13,8 @@ class User extends Authenticatable implements MustVerifyEmail
{ {
use Notifiable; use Notifiable;
public static $rules=['cpf' =>'required'];
public static $messages=['cpf'=>'erro cpf'];
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *
......
<?php
namespace App\Validator;
class CpfValidator
{
public static function validate($data)
{
$validator = \Validator::make($data, ['cpf' => 'required'], ['cpf' => 'erro cpf']);
$cpf = preg_replace('/[^0-9]/', '', (string)$data['cpf']);
if (strlen($cpf) != 11) {
$validator->errors()->add('cpf', 'Necessário 11 números em um CPF');
throw new ValidationException($validator, "Erro");
}else{
for ($i = 0, $j = 10, $soma = 0; $i < 9; $i++, $j--) {
$soma += $cpf[$i] * $j;
}
$resto = $soma % 11;
if ($cpf[9] != ($resto < 2 ? 0 : 11 - $resto)) {
$validator->errors()->add('cpf', 'CPF Inválido');
}
for ($i = 0, $j = 11, $soma = 0; $i < 10; $i++, $j--) {
if (str_repeat($i, 11) == $cpf) {
$validator->errors()->add('cpf', 'CPF Inválido');
}
$soma += $cpf[$i] * $j;
}
$resto = $soma % 11;
if ($cpf[10] != ($resto < 2 ? 0 : 11 - $resto)) {
$validator->errors()->add('cpf', 'CPF Inválido');
}
if(!$validator->errors()->isEmpty())
throw new ValidationException($validator, "Erro");
return true;
}
}
}
<?php
namespace App\Validator;
class ValidationException extends \Exception
{
protected $validator;
public function __construct($validator,$text ="Erro na validação")
{
parent::__construct($text);
$this->validator = $validator;
}
public function getValidator(){
return $this->validator;
}
//
}
...@@ -245,15 +245,17 @@ ...@@ -245,15 +245,17 @@
@endcomponent @endcomponent
</div> </div>
<div class="col-6"> <div class="col-6 {{ $errors->has('cpf') ? ' has-error' : '' }}">
@component('componentes.input', ['label' => 'CPF']) @component('componentes.input', ['label' => 'CPF'])
<input type="text" class="form-control cpf" value="" name="cpf" placeholder="CPF" id="cpf{{$participante->id}}" required /> <input type="text" class="form-control cpf @error('cpf') is-invalid @enderror" value=""
onchange="checarCPFdoCampo(this)"
@error('cpf') name="cpf" placeholder="CPF" id="cpf{{$participante->id}}" required autofocus autocomplete="cpf"/>
<span class="invalid-feedback" role="alert" style="overflow: visible; display:block">
<strong>{{ $message }}</strong> @error('cpf')
</span> <span class="help-block">
@enderror <strong>{{ $message }}</strong>
</span>
@enderror
@endcomponent @endcomponent
</div> </div>
<div class="col-6"> <div class="col-6">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment