Forms with Laravel
Intro
- Create the class diagram of your application
Part 1 – creation of the booking form
Deliverable : otelo/mission3/…
- create a view createReservation
@extends('layouts.app') @section('content') <div class="container"> <form method="post" action="{{ route('reservation.store')}}"> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"> <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> </div> <div class="form-check"> <input type="checkbox" class="form-check-input" id="exampleCheck1"> <label class="form-check-label" for="exampleCheck1">Check me out</label> </div> <div class="form-group"> <label for="sel1">Select list:</label> <select name="idperiode" class="form-control" id="sel1"> <option value=1>basse</option> <option value=2>moyenne</option> <option value=3>haute</option> </select> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> @stop
- create the controller ReservationController
- modify the function create of ReservationController
return view('createReservation');
- add the route
Route::get('/newreservation',[ReservationController::class, 'create'] );
- test the form, an error regarding the view is generated, suppress the action reservation.store in the form.
- adapt the form to contain the necessary fields, we will not generate booking lines (no id chambre)
Part 2 – retrieval and registration
Deliverable : otelo/mission3/…
- add the action reservation.store in the form
- add the retrieval route
Route::post('/storereservation',[ReservationController::class, 'store'] )->name('reservation.store');
- test the form, then format it so that it contains the necessary fields. We will not manage booking lines (no id chambre)
- add to the store function of the controller
dd($request);
- test, a 419 error is generated, you have to manage a session token.
- add, after the form tag, @csrf
<form method="post" action="{{ route('reservation.store')}}"> @csrf
- observe the token generated in the form via the browser
- check the data in the display, afetr approval of the form
- create the Reservation.php model with the right fields, beware the capital letters in the fields of the table
- modify the store method of ReservationController.php, check that the inputs have the names dated datef and idperiode.
$reservation=new Reservation(); $dated=$request->input('dated'); $datef=$request->input('datef'); $idperiode=$request->input('idperiode'); $reservation->dateD=$dated; $reservation->dateF=$datef; $reservation->idPeriode=$idperiode; $reservation->save(); return redirect()->back();
- test, 2 fields are missing, add them then re-test.
Part 3 – control the back-end inputs
Deliverable : otelo/mission3/…
- Modify the store function of ReservationController to indicate that your fields are mandatory
public function store(Request $request) { $validatedData = $request->validate([ 'dated' => 'required|date|after:tomorrow', 'datef' => 'required|date|after:datef', 'idperiode'=> 'required|between:1,3' ]); $reservation=new Reservation(); ...... }
- Modify the view createReservation
@extends('layouts.app') @section('content') <div class="container"> @if($errors->any()) <div class='alert alert-danger'> <ul> @foreach($errors->all() as $error) <li>{{$error}}</li> @endforeach </ul> @endif <form method="post" action="{{ route('reservation.store')}}"> ......
- Test
- Modify the view createReservation
@extends('layouts.app') @section('content') <div class="container"> <form method="post" action="{{ route('reservation.store')}}"> @csrf <div class="form-group"> <label for="exampleInputEmail1">date debut</label> <input name="dated" type="date" class="form-control @error('dated') is-invalid @enderror" id="exampleInputEmail1" > @error('dated') <div class="alert alert-danger mt-2"> {{$message}} mon message perso </div> @enderror </div> <div class="form-group"> <label for="exampleInputPassword1">date fin</label> <input name="datef" type="date" class="form-control" id="exampleInputPassword1" > </div> <div class="form-group"> <label for="sel1">Select list:</label> <select name="idperiode" class="form-control" id="sel1"> <option value=1>basse</option> <option value=2>moyenne</option> <option value=3>haute</option> </select> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> @stop
- Test
- Validate the fields datef and idperiode on the back-end side
- Validate the fields on the front-end side