Anastasie Laurent
Anastasie Laurent

Reputation: 1179

laravel form post instead of put

This is my form:

{{ Form::model($data, array(
    'route' => array('waitingtimes.update', $data->id),
    'class' => 'mainInformationContrainer',
    'method' => 'put'
)) }}

When I submit the form, I got

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

though I've already set the request as put.

Could you help me please?

Edit 1

I noticed that the form html is

<form method="POST" action="http://localhost:8082/test/public/waitingtimes/2" accept-charset="UTF-8" class="mainInformationContrainer">
</form>

It is post not put,

Edit 2

The problem was because I mistyped the route to rout, but not I am getting this exception

Trying to get property of non-object

this is the view:

{{Form::model($data, array(
'route' => array('waitingtimes.update', $data->id)
, 'class' => 'mainInformationContrainer',
'method' => 'put'
))}}
<ul>
    <li>
        <label>First Time:</label>
        <div class="oneInfo">
            {{ Form::text('startTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'startTime', 'autocomplete' => 'off'))}}
            <span class="errorMessage">
                <?php
                echo $errors->first('startTime');
                ?>
            </span>
        </div>
    </li>
    <li>
        <label>End Time:</label>
        <div class="oneInfo">
            {{Form::text('endTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'endTime'))}}
            <span class="errorMessage">
                <?php
                echo $errors->first('endTime');
                ?>
            </span>
        </div>
    </li>
    <li>
        <label>Value:</label>
        <div class="oneInfo">
            {{Form::text('value', $value=null, array())}}
            <span class="errorMessage">
                <?php
                echo $errors->first('value');
                ?>
            </span>
        </div>
    </li>
    <li>
        <input type="submit" value="Save Changes"/>
        <input type="button" value="Cancle" class="cancelButton"/>
    </li>
</ul>
{{ Form::close() }}

this is the controller update

$input = Input::all();
        $validation = Validator::make($input, WaitingTimes::$rules);
        if ($validation->passes()){}else{
            return Redirect::route('waitingtimes.edit')->withInput()->withErrors($validation)->with(array(
                'verticalMenu'=>'none',
                'verticalMenuTab' => 'none',
                'data' => $input
            ));
        }

Please notice that this html blade code is used for editing the data and it is working correct when I call the edit function, and I am using it also to redirect when the user try to edit information but the validation falls

Upvotes: 1

Views: 1898

Answers (3)

Anastasie Laurent
Anastasie Laurent

Reputation: 1179

I found the solution,

which is

return Redirect::back()->withInput()->withErrors($validation)->with(array(
                'verticalMenu'=>'none',
                'verticalMenuTab' => 'none',
                'data' => $input
            ));

Thanks to this question Laravel form model binding

Upvotes: 1

Unnawut
Unnawut

Reputation: 7578

You will need to tell your form that you will be using method PUT:

{{ Form::model($data, array(
    'route' => array('waitingtimes.update', $data->id),
    'class' => 'mainInformationContrainer',
    'method' => 'put',
)) }}

Note that you will still see method = "POST" in your form but Laravel will add a hidden field called _method to your form. See http://laravel.com/docs/html#opening-a-form

Upvotes: 2

har2vey
har2vey

Reputation: 676

You'll need to specify the method in your form creation, add this to your Form::model array:

'method' => 'PUT'

Upvotes: 2

Related Questions