Tuesday, May 23, 2017

Laravel Form Validation on POST request

I have a form which is divided into 2 parts. The first part of the form is displayed with "GET" request when the user clicks a button. when the user fills this form and clicks next, the page is redirected to the next part of the form with a "POST" request which first saves the form and then displays the next part. The problem is when i try to validate the 2nd part of the form, laravel gives and error saying method not allowed.

my first controller:

public function create(Request $request)
{

    $validator = Validator::make($request->all(), [
        'unit_code' => 'max:25',
        'unit_title' => 'max:255',
        'project_title' => 'max:255',
        'project_ref_number' => 'max:255',
        'storage_location' => 'required|max:255',
        'keeper_name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        return redirect()->back()
                    ->withErrors($validator)
                    ->withInput();
    }

    $notification = Notification::create([
        'unit_code'         =>$request->unit_code,
        'unit_title'        =>$request->unit_title,
        'project_title'     =>$request->project_title,
        'project_ref_number'=>$request->project_ref_number,
        'storage_location'  =>$request->storage_location,
        'keeper_name'       =>$request->keeper_name,
        'user_id'           =>Auth::user()->id
        ]);

    return view('Notification.notification_for_lmo')->with('notification', $notification);
    //return redirect()->route('show.material_List')->with('notification'); 

}

routes:

/*route to personal information form for notification*/
    Route::get('/personal_information_notification_form', 'HomeController@getNotificationForm');

    /*submit personal information and go to next part of the form*/
    Route::post('personal_information_notification_form/submit/', 'NotificationController@create')->name('submit.personal_info_for_notification');

    /*route to material list form for notification application*/
    Route::get('personal_information_notification_form/material_list/', 'NotificationController@showMaterialListForm')->name('show.material_List');

    /*submit the material list*/
    Route::post('/personal_information_notification_form/add_lmo/', 'NotificationTypeAController@create')->name('submit.add_lmo');

2nd controller:

public function create(Request $request)
{
    /*this loop is because im adding rows dynamically to the table*/
    $count = count($request->input('item_name'));

    for ($i=0; $i<$count; $i++){

        $validator = Validator::make($request->all(), [
            'material_type' => 'required|max:25',
            'item_name' => 'required|max:255',
            'risk_level' => 'required|max:255',
            'quantity' => 'required|max:255',
            'volume' => 'required|max:255',
            'notification_id' => 'required|max:255',
        ]);

    }

    if ($validator->fails()) {
        return view('Notification.notification_for_lmo')->withErrors($validator)->withInput();
    }

    for ($i=0; $i<$count; $i++){

        $data  = NotificationTypeA::create([
            'material_type'  =>$request->material_type[$i],
            'item_name'      =>$request->item_name[$i],
            'risk_level'     =>$request->risk_level[$i],
            'quantity'       =>$request->quantity[$i],
            'volume'         =>$request->volume[$i],
            'notification_id'=>$request->notification_id
        ]);
    }



        $admin = Admin::find(1);
        $user = Auth::user();
        $notification = Notification::find($request->notification_id);
        $admin->notify(new NewNotificationApplicationSubmitted($user->name, $notification->id));

        return redirect()->route('show.go_to_notification')->with('message', 'We have notified '.$user->name.' that he/she is added to SSBC')->with('status', 'info');

    }

In the second controller, there is a for loop because the user fills the data in a table form. (the blade file is big so i am not posting it here.)

What is the problem with this and why is this happening?

first blade:

<div class="well">
                    <form class="form-horizontal" role="form" method="POST" action="">
                        

                        <fieldset>
                            <legend>
                                SECTION 1 - Personal Details
                            </legend>
                            <div class="row">
                                <div class="col-xs-12">

                                    <div class="form-group">
                                        <div class="col-xs-12">
                                            {!! Form::label('unit_code', 'Unit Code (if teaching):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('unit_code', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('unit_title', 'Unit Title (if teaching):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('unit_title', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('project_title', 'Project Title (if FYP/research):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('project_title', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('project_ref_number', 'Ref. No (if FYP/research):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('project_ref_number', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('storage_location', 'Storage Location:', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('storage_location', $value = null, ['class' => 'form-control' ]) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('keeper_name', 'Name of the Keeper:', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('keeper_name', $value = null, ['class' => 'form-control' ]) !!}
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </fieldset>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary btn-block">Next</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

2nd blade:

<form class="form-horizontal" role="form" method="POST" action="">
                        

                        <fieldset>
                            <div class="row">
                                <div class="col-xs-12">
                                    <div class="row">
                                        <legend>
                                            SECTION 2 – Details of the Biohazardous Materials
                                        </legend>
                                        <div class="col-xs-8">
                                            <h4>List of Living Modified Organism (LMO)</h4>
                                        </div>
                                        <div class="col-xs-4">
                                            <input type="checkbox"/>Applicable
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-xs-12">
                                            <div id="LMOtablediv">
                                                <input type="button" id="addmoreLMObutton" value="Add" onclick="insRow(event)" />
                                                <table id="addLMOtable" border="1">
                                                    <thead>
                                                        <tr>
                                                            <td>No.</td>
                                                            <td>Notification ID</td>
                                                            <td>Material Type</td>
                                                            <td>Name</td>
                                                            <td>Risk Level</td>
                                                            <td>Quantity</td>
                                                            <td>Volume</td>

                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td>1</td>
                                                            <td><input type="text" name="notification_id" id="notification_id" value=""></td>
                                                            <td>{!! Form::text('material_type[]', null, array('id'=>'material_type'))!!}</td>
                                                            <td>{!! Form::text('item_name[]', null, array('id'=>'item_name'))!!}</td>
                                                            <td>{!! Form::text('risk_level[]', null, array('id'=>'risk_level'))!!}</td>
                                                            <td>{!! Form::number('quantity[]', null, array('id'=>'quantity'))!!}</td>
                                                            <td>{!! Form::number('volume[]', null, array('id'=>'volume'))!!}</td>

                                                            <td><input type="button" id="delLMObutton" value="Delete" onclick="deleteRow(this)"/></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

                        <div class="row">
                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-10">
                                    <button type="submit" class="btn btn-primary">Submit</button>
                                </div>
                            </div>
                        </div>
                    </form>



via Mill3r

Advertisement