1
\$\begingroup\$

I have just started playing around with L4 and would like to get some feedback on how I implemented a multi-page form

Here are my routes:

Route::model('appointment', 'Appointment');
Route::get('appointment/book_new/{appointment}', array('as' => 'booking-form', 'uses' => 'AppointmentBookingController@index') );
Route::post('appointment/book_new/verify/{appointment}', 'AppointmentBookingController@verify');
Route::post('appointment/book_new/success/{appointment}', 'AppointmentBookingController@success');

Here is my controller:

class AppointmentBookingController extends BaseController {

    public function index(Appointment $appt)
    {                   
        return View::make('appointment/bookings/book',  array('appt' => $appt));
    }

    public function verify(Appointment $appt){
        $fields = array('name', 'email', 'description');

        // Declare the rules for the form validation
        $rules = array(
            'name'        => 'required',
            'email'       => 'required|email',
            'description' => 'required',
        );

        // Create a new validator instance from our validation rules
        $validator = Validator::make(Input::all(), $rules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails())
        {           
            foreach($fields as $v)
            {
                Session::flash($v, Input::get($v));
            }           
            return View::make('appointment/bookings/book',  array('appt' => $appt, 'errors' => $validator->messages()));
        } else {
            Session::put('booking-form-index', Input::all());
            return View::make('/appointment/bookings/verify',  array('appt' => $appt));
        }
    }

    public function success(Appointment $appt){
        // validate, if valid save the booking.
            // don't do anything for now
            var_dump ( Session::get('booking-form-index') );
            exit();

            // display the success page
            // return View::make('/appointment/bookings/success',  array('appt' => $appt)); 
}

Here are my views ( book.blade.php )

{{ Form::open(array('url' => '/appointment/book_new/verify/'. $appt->id )) }}

<!-- CSRF Token -->
{{ Form::token() }}

<!-- Name -->
<div>
    {{ Form::text('name', Session::get('name'), array('placeholder' => 'Name')) }}  
    {{ $errors->first('name') }}
</div>

<!-- Email -->
<div >
    {{ Form::text('email', Session::get('email'), array('placeholder' => 'email@email.com')) }}  
    {{ $errors->first('email') }}
</div>
<!-- Description -->
<div >
    {{  Form::textarea ('description') }}
    {{ $errors->first('description') }}
</div>

<!-- Form actions -->
{{ Form::submit('Click Me!') }}

{{ Form::close() }}

verify.blade.php

{{ Form::open(array('url' => 'appointment/book_new/success/' . $appt->id )) }}

{{  Form::text('validate') }}

{{ Form::submit('Click Me!') }}   

{{ Form::close() }}

success.blade.php

Success
\$\endgroup\$
1
\$\begingroup\$

Looks pretty standard to me! The only minor tweak I would make just for readability is to eliminate the else in AppointmentBookingController::verify(); since you return in the if, the else block is not necessary, and using guard clauses like this will (arguably) make your code more readable. See Invert "if" statement to reduce nesting.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy