laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Form::open should use the route to determine the method on Resource routes. #616

Closed atrauzzi closed 11 years ago

atrauzzi commented 11 years ago

Right now, I have to add the 'method' => 'PUT' parameter and others based on what I want going on. Although if it's going to accept a route, it would be pretty slick if it could infer that too!

andrewryno commented 11 years ago

GET, PUT/PATCH, and DESTROY can reference the same route. You can't infer which method you mean just based on a route.

atrauzzi commented 11 years ago

Hmm, not necessarily when using resource routes:

<?php echo Form::open(array('route' => 'user.store')); ?>

This implies the method and could offer an opportunity for a shortcut.

andrewryno commented 11 years ago

Ohhh a named route. Yeah. I was thinking as in you were specifying '/user/1' or something. My bad. :)

bencorlett commented 11 years ago

Curious, when are you going to be able to use anything but GET and POST on a HTML form?

I think people are getting way too obsessive over this form class, I think a separation of concerns is nice. Markup should be left as markup and not generated by a PHP class, but that's just my opinion.

andrewryno commented 11 years ago

@bencorlett the Form class will automatically add the _method hidden field when you specify a method other than GET/POST.

See:

https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php#L84 https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php#L93

JonoB commented 11 years ago

:+1:

Regulus343 commented 11 years ago

The Formation package I wrote (pre-Laravel 4 Form class) based on L3's form class does this. You can find it at http://github.com/Aquanode/Formation. You might be able to grab some stuff from the Form::resource() method and get it added to L4's native Form class. I have yet to add in all the new L4 Form class methods to Formation, but I will get around to it eventually.

DavidOliver commented 11 years ago

I'm a relative beginner at programming and even newer to frameworks, but I was somehow expecting not to have to set the action in this situation.

I'm using a resourceful controller and bound my form to the model:

{{ Form::model($wishlist, array('action' => array('WishlistsController@update', $wishlist->id))) }}

But of course I did have to set the method:

{{ Form::model($wishlist, array(
    'action' => array('WishlistsController@update', $wishlist->id),
    'method' => 'put'
)) }}

So I'd also be happy to see the method handled automatically for restful/resourceful controllers if it doesn't have negative implications.

As marxtel in IRC pointed out, it would mean the same form could be used for create and edit views.

StudentDeveloper commented 11 years ago

+1, in addition, why don't you make the "$wishlist->id" optional. So

{{ Form::model($wishlist, array(
    'action' => array('WishlistsController@update', $wishlist->id),
    'method' => 'put'
)) }}

Would be the same as

{{ Form::model($wishlist, ['action' => ['WishlistsController@update']] ) }}
taylorotwell commented 11 years ago

I would prefer to leave any further, more advanced form stuff up to packages. It's a little distracting from the "core" framework stuff.