1

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

Anastasie Laurent
  • 1,169
  • 6
  • 23
  • 35
  • Yes, HTML forms only accept POST and GET methods, laravel checks the value of the posted `_method` property, laravel form helpers create a hidden input for this (``). It's a post request but it calls the put handler. – Ram Jun 24 '14 at 08:50
  • @undefined so why the exception happens please? – Anastasie Laurent Jun 24 '14 at 08:52
  • Can you also post your `Routes`? – Unnawut Jun 24 '14 at 08:52
  • @Unnawut it is easy `Route::resource('waitingtimes', 'WaitingtimesController');` – Anastasie Laurent Jun 24 '14 at 08:53
  • `Input::all()` returns an array, you are treating it as an object, `$data->id`, it's supposed to be `$data['id']`? – Ram Jun 24 '14 at 09:07
  • @undefined I know that but I must say `$data->id` because as I told you i ma using the same html to both edit and redirect after edit if the validation falls. so what should I do in the edit controller when the validation falls in order to change the array of data to an object of data? I guess here is the solution? am i right? – Anastasie Laurent Jun 24 '14 at 09:09
  • You can cast it to object: `(object) $data`, please check this question http://stackoverflow.com/questions/1869091/convert-array-to-object-php – Ram Jun 24 '14 at 09:11
  • @undefined before i apply the cast thing, I guess laravel should have something built for that right? i mean why going with working arround while the framework alreayd has a solution. laravel should have something to redirect the request with the requested model, am i right ? – Anastasie Laurent Jun 24 '14 at 09:14
  • @undefined I found the solution, please check my answer – Anastasie Laurent Jun 24 '14 at 09:20

3 Answers3

2

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

'method' => 'PUT'
har2vey
  • 676
  • 6
  • 19
  • I already did that put I forgot to put that in the question, the result is the same – Anastasie Laurent Jun 24 '14 at 08:50
  • 1
    So did you get the hidden field name "_method" in the form? (it's generated by laravel to spoof the method PUT) – har2vey Jun 24 '14 at 08:58
  • No, I mean to check the view after you run the code, then click 'view source' on your browser to check the form element on runtime to see whether the hidden form name "_method" with value "put' is added. – har2vey Jun 24 '14 at 09:09
  • there is no problem with the put or post now, i edited the question, the problem was something else, i just was typo the `rout` wrong – Anastasie Laurent Jun 24 '14 at 09:12
  • Good for you then, btw just a suggestion, you might wanna use {{{$errors->first('startTime')}}} for all your errors in your view instead of echo for a cleaner code. – har2vey Jun 24 '14 at 09:13
2

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

Unnawut
  • 7,500
  • 1
  • 26
  • 33
  • I already did that but it was a typo, i forgot to put that in the question, then I put it in the question but someone edited the question and removed it. – Anastasie Laurent Jun 24 '14 at 08:51
1

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

Community
  • 1
  • 1
Anastasie Laurent
  • 1,169
  • 6
  • 23
  • 35
  • Laravel also supports [Route model binding](http://laravel.com/docs/routing#route-model-binding). – Ram Jun 24 '14 at 09:18