Cordial regards colleagues, it turns out that I am developing an application in laravel that so far allows me to perform a full crud of clients, to create and edit clients I use bootstrap and jquery modal windows, the problem that I present is that in the controller I use the $ this- > validate, to do the respective validations of the form that is in the modal windows, when an error occurs in the data entry the $ this- > validate me returns to the same view and it shows me the error in the form, but in this case, I'm using modal windows I had to make a conditional in the blade view, so that when a validation error occurs I will return to the same view and open the modal to visualize the error, this validation is the following:
{{-- Con este condicional abrimos el modal si hay un error de validacion en el backend --}}
@if($errors->any())
<script>
$('#createclient').modal('show');
</script>
@endif
The problem is that it is only working with the modal to create a client. How could I validate that, when there is an error in the form to create, I return to the view with the modal to create open and when there is an error in the form to edit I return to the view with the modal of editing open?
I attached the complete blade view code:
@extends('layouts.app')
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
@section('content')
<div class="container" style="text-align:right">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#createclient">
Añadir cliente
</button>
</div>
<br>
<div class="container-fluid">
<!-- Modal para agregar cliente -->
<form action="{{ route('client.create') }}" method="POST">
{{ csrf_field() }}
<div class="modal fade" id="createclient" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="modalLabel">Crear cliente</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-content">
<div class="container-fluid">
<span class="input-group-text">
<strong>Nombre del cliente</strong>
</span>
<input type="text" class="form-control" name="name" id="name" value="{{ old('name') }}" placeholder="Ingrese el nombre del cliente" required>
<br> {{ $errors->first('name') }}
<span class="input-group-text">
<strong>Correo del cliente</strong>
</span>
<input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}" placeholder="Ingrese el correo del cliente" required>
<br> {{ $errors->first('email') }}
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Crear</button>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="container-fluid">
<table class="table table-hover table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">NOMBRE</th>
<th scope="col">CORREO</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
@foreach($client as $cliente)
<tr>
<td>{{ $cliente->name }}</td>
<td>{{ $cliente->email }}</td>
<td>
<button type="button" data-id="{{ $cliente->id }}" class="btn btn-primary" data-toggle="modal" data-target="#editar_{{$cliente->id}}">
<i class="fa fa-edit"></i>
</button>
<!-- Modal para editar cliente -->
<div class="modal fade" id="editar_{{$cliente->id}}" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<form action="{{ route('client.update',$cliente->id) }}" method="POST">
{{ csrf_field() }} {{ method_field('PUT') }}
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="modalLabel">Editar cliente</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-content">
<div class="container-fluid">
<input type="text" name="id" value="{{ $cliente->id }}" hidden>
<span class="input-group-text">
<strong>Nombre del cliente</strong>
</span>
<input type="text" class="form-control" name="name" id="name" value="{{ $cliente->name }}" placeholder="Ingrese el nombre del cliente" required>
<br> {{ $errors->first('name') }}
<span class="input-group-text">
<strong>Correo del cliente</strong>
</span>
<input type="email" class="form-control" name="email" id="email" value="{{ $cliente->email }}" placeholder="Ingrese el correo del cliente" required>
<br> {{ $errors->first('email') }}
<input type="text" name="user_id" value="{{ auth()->user()->id }}" hidden>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Editar</button>
</div>
</div>
</form>
</div>
</div>
{{-- boton para eliminar --}}
<form method="POST" action="{{ route('client.destroy', $cliente->id) }}" style="display:inline">
{{ csrf_field() }} {{ method_field('DELETE') }}
<button class="btn btn-xs btn-danger" onclick="return confirm('¿Estas seguro de querer eliminar este cliente?')">
<i class="fa fa-times"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
{{-- Con este condicional abrimos el modal si hay un error de validacion en el backend --}}
@if($errors->any())
<script>
$('#createclient').modal('show');
</script>
@endif
@stop
I also attach the functions of creating and editing the controller just in case:
store function to create:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:clients,email'
]);
toastr()->success('Correctamente', 'Cliente creado', [
'timeOut' => 2000,
'positionClass' => "toast-top-full-width",
'progressBar' => false,
'showDuration'=> 300,
]);
//De esta forma se inserta tambien el id del usuario al que pertenece el cliente en la bd
auth()->user()->client()->create($request->all());
return redirect()->route('client.list');
}
update function to update or edit the client:
public function update(Request $request, Client $client)
{
//Consultamos el email perteneciente al cliente
$email = $client->email;
//Si el email que llega por request, es igual al de la base de datos
if ($request->email == $email) {
$request->request->remove('email');
}
$this->validate($request, [
'email' => 'unique:clients'
]);
toastr()->info('Correctamente', 'Cliente editado', [
'timeOut' => 2000,
'positionClass' => "toast-top-full-width",
'progressBar' => false,
'showDuration'=> 300,
]);
return redirect()->route('client.list');
}