Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimeSocks's avatar

How can I solve this validation/routing problem?

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?

0 likes
2 replies
pmall's avatar
pmall
Best Answer
Level 56

Of course when you get back to the page the ticketno is not present in the request. Save it to the session if it is present.

if ($request->has('ticketno')) session()->put('ticketno',$ticketno);

$ticketno = session()->get('ticketno');

If request has no ticketno (redirecte back) it uses the one from session.

Please or to participate in this conversation.