I have an app where a customer can book tickets for an event. The first step of the booking process is to choose how many tickets they want. The app then uses this number to generate that number of inputs to take the details of each ticket buyer.
I've hit a snag with the form request validation and the way it redirects back to the page. Firstly, here's the code:
Routes
Route::get('book/{id}','BookController@show');
Route::post('book/{id}/yourdetails','BookController@yourDetails');
Route::get('book/{id}/yourdetails','BookController@yourDetails');
Route::post('book/{id}/summary','BookController@summary');
Controller
public function yourDetails(Request $request)
{
$ticketno = $request->all();
$instance = session()->get('instance');
session()->put('ticketno',$ticketno);
return view('book.yourdetails',compact(['ticketno','instance']));
}
public function summary(Requests\YourDetailsRequest $request)
{
$data = $request->all();
$instance = session()->get('instance');
$ticketno = session()->get('ticketno');
return view('book.summary',compact(['data','instance','ticketno']));
//return $data;
}
View:
{!! Form::open(['method' => 'POST','action' => 'BookController@summary']) !!}
<div class="form-group">
{!! Form::label('booker_name','Your Name:') !!}
{!! Form::text('booker_name', null, ['class' => 'form-control', 'id' => 'booker_name']) !!}
@if ($errors->has('booker_name')) <p class="alert alert-danger">Your name is required</p> @endif
</div>
<div class="form-group">
{!! Form::label('booker_tel','Your telephone number:') !!}
{!! Form::text('booker_tel', null, ['class' => 'form-control', 'id' => 'booker_tel']) !!}
@if ($errors->has('booker_tel')) <p class="alert alert-danger">Your telephone number is required</p> @endif
</div>
<div class="form-group">
{!! Form::label('booker_email','Your E-mail Address:') !!}
{!! Form::text('booker_email', null, ['class' => 'form-control', 'id' => 'booker_email']) !!}
@if ($errors->has('booker_email')) <p class="alert alert-danger">Your e-mail address is required and must be correctly formatted</p> @endif
</div>
<hr/>
@for($i = 0; $i < $ticketno['ticketno']; $i++)
<div class="form-group">
{!! Form::label('tickets['. $i .'][name]','Name:') !!}
{!! Form::text('tickets['. $i .'][name]', null, ['class' => 'form-control', 'id' => 'ticket_name_'.$i]) !!}
</div>
<div class="form-group">
{!! Form::label('tickets['. $i .'][email]','E-mail Address:') !!}
{!! Form::text('tickets['. $i .'][email]', null, ['class' => 'form-control', 'id' => 'ticket_email_'.$i]) !!}
</div>
<div class="form-group">
{!! Form::label('tickets['. $i .'][tel]','Telephone Number:') !!}
{!! Form::text('tickets['. $i .'][tel]', null, ['class' => 'form-control', 'id' => 'ticket_tel_'.$i]) !!}
</div>
@endfor
Form Request:
class YourDetailsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'booker_name' => 'required',
'booker_tel' => 'required|numeric',
'booker_email' => 'required|email',
];
}
}
The problem I have occurs when validation fails. The app redirects back to /book/{id}/yourdetails, and throws this error:
Undefined index: ticketno.
Basically it seems like it's losing the ticketno by POSTing the request to the next page (/summary). How can I fix it?