I'm currently trying out Laravel 4 and I have created a resource controller. In the 'edit' function I'm building a form, which should post to the 'update' function.
To create the form open tag I use the Form::open() function which recently got added to Laravel 4 it seems.
But when I just do Form::open() the action of the form is the current url and I can't figure out how to change the action.
I tried Form::open('clients/' . $client->id) but this gives me the following error:
ErrorException: Catchable Fatal Error: Argument 1 passed to Illuminate\Html\FormBuilder::open() must be of the type array
So I tried Form::open('[clients/' . $client->id). This doesn't generate an error, but now the form open tag is:
<form method="POST" action="http://boekhouding.dev/clients/1/edit" accept-charset="UTF-8" clients/1="clients/1">
And I also tried it like this: Form::open(['action' => 'clients/' . $client->id]) but when I do it like this, the form open tag has no action at all.
So, does anyone know how to set the form action? Using a named route would be perfect, but even being able to set the action at all would be nice.
You can use named route, controller action or simple url to set form action.
To set it via named route use:
{{ Form::open(array('route' => array('route_name', $client->id))) }}
To set it via controller action use:
{{ Form::open(array('action' => array('ClientController#update', $client->id))) }}
So the keyword action does not reffer to 'action' parameter of form tag, but to controller action
And you can also use plain URL like this:
{{ Form::open(array('url' => 'someurl')) }}
#jeffrey_way tweeted about improving the new FormBuilder in Laravel 4. The following paste bucket link should help. It seems to be more about RESTful controllers, but relavant.
Form action sensible defaults - paste bucket
I thought I read something about him coming out with a Forms tutorial tomorrow. If so, it might be found here net.tutsplus.com/?s=laravel
Related
In the form I have to define a handler like admin/login. Then it changes the page when I send it. I also tried using public function indexAction() handler and only using the controller admin without a handler, but then nothing happens.
Any help would be greatly appreciated.
Edit:
I wanted to have a error message but it was not so big a site that i wanted to make a form class.
The way i eventually fixed it was the following:
First i use $this->view->pick('admin/index'); in the handler to select my old view.
Next i send a variable like a error message with $this->view->setVars(['username' => $user->name,]); in the handler.
Last i used it with volt in the previous view views/admin/index.phtml like this {{ username }}
If you are using a form object you can then use the following code in your volt template
{{ form('myform', 'method':'post', 'role': 'form') }}
This will post your data to the same controller/action that invoked the form. From then you will be able to handle the posted data using valid() in the form as well as request->isPost()
I'm trying to access my "store" (POST) method from my route resource. I call the ProductCRUD.store method from a form that can have different input depending on the previous page. I mean by that, depending if the object i receive have certain boolean value, the input is shown.
#if($category->havePrice == 1)
{!! Form::label('price', 'Price') !!}
{!! Form::text('price', '', array('id' => 'price', 'name' => "price") !!}
#endif
When I click on the button to send the form, I got a message saying
MethodNotAllowedHttpException in RouteCollection.php line 251:...
I tried to add a csrf token with the method csrf_field() too.
I tried to empty the cache of laravel with those two methods:
php artisan cache:clear
php artisan route:cache
I tried to create a personal route like this:
Route::post('/product/store', array('as' => 'product', 'uses' => 'ProductController#store'));
None of those solution works. Always that exception.
My current code:
Route:
Route::resource('ProductCRUD','ProductController');
Form
<form method="POST" action="{{route('ProductCRUD.store')}}">
{{ csrf_field() }}
Controller method:
public function store(Request $request) {
$newGameModel= new GameModel();
$category = $this->CategoryRepo->getByID($request->category);
$this->validate($request, [
'name'=> 'required',
'picture'=> 'required|image|mimes:jpg,jpeg,gif,png|max:5000',
'quantite' => 'required|min:0',
'price' => 'required_if:'.$category->havePrice.',1',
]);
...
}
If I remove the "$this->validate(...)" method the code continues to execute (no more MethodNotAllowedException error). I tried to comment one by one validation it changes nothing.
Plus, if I reload the page, the input error are shown correctly. If i click again to send the form, surprise! The error again.
Edit:
I send my form using only a button and the form, no ajax:
{!! Form::submit('Next step', 'name'=>"accept", 'methode' => "post")) !!}
Plus, I can access my controller code. If I remove the validate method, everything work find
Do you guys have any idea why it's doing that? Thank you for your time!
Ok, so I found the problem and how to fix it (not what I wanted to do).
First, I don't have any error in my code. My post, csrf and my routes are all good.
The problem is how larval validates things. For example, If your first page was a post form and your second page is also a post form, when you validate on the second page and it fails, it will try to return to the second page to show the error. But the fact is, it can't find the response of your first page, so you got an error like I got.
I fix it by removing the first form (it was, for me, a little information I can put somewhere else). But I don't know if there is a better way to fix it.
There is a form in my view, from where, on clicking submit, data is taken to ajax script which is supposed to invoke the controllers specified in the post routes in routes.php. The controllers are expected to process the received data and throw the result back to the view. Why is this error occurring immediately after submitting the form and things aren't working as expected>
Check your routes.php file. Make sure that the method your using corresponds with what you have allowed.
Here is the laravel documenation for routes
For example, if your request is a GET request, the call should look like this in the routes file:
Route::get('/test-get-url', function () {
// Matches The "/test-get-url" URL using a GET method
});
And for post
Route::post('/test-post-url', function () {
// Matches The "/test-post-url" URL using a POST method
});
Go to terminal/cmd..whichever you using and type
php artisan route:list
this will list all your routes and check at what route your is being submitted.Note the corresponding method to be used in the Method column and use that method while submitting form.I'm assuming it's PUT method(which is most likely).Ex--
Use method attribute if you're using simple HTML code like--
<form action="{{route='..'}}" method="PUT">
or if you're using form helper then use--
{!! Form::open($post, ['route' => ['..'], 'method' => 'PUT']) !!}
Go to your route.php file and check
Route::get('someroute',['uses'=>'somecontroller#function_get','as'=>'some_get']);
Route::post('someroute',['uses'=>'somecontroller#funtion_post','as'=>'some_post']);
make sure if your are using (as) in you routes its different and also where you use your ajax you properly define your route where you want to post your form data and it's type is same as you mention in routes and one more thing check you properly use (;) in your code
hope this will help you fell free to ask your query
I have this form:
{!!Form::open(['route'=>'fastsearch.show'])!!}
In routes.php I have:
Route::resource('fastsearch','SearchController');
And in SearchController i have a method show() that sends the return to a view called fastsearch (which is fastsearch.blade.php)
If I look into the source of the page where the form is, I see this:
<form method="POST" action="http://localhost:8000/fastsearch/%7Bfastsearch%7D" accept-charset="UTF-8"><input name="_token" type="hidden" value="hLcSkGk2p5XfTkFEv2pwGgcVQB18vHQIGMpOVGpM">
If I put some data in the form and click Submit, I get this error:
MethodNotAllowedHttpException in RouteCollection.php line 201:
My question is why the extra segment in the action URL (this one: /%7Bfastsearch%7D). Is something wrong with the routes ?
(Just to give you all the details, this a general search form that lies on almost every page in order to allow the users to run a quick search from almost every page they might be at that time. So it doesn’t matter if you’re on Home page or on /Home/Subpage/SubSubPage{wildcard}{wildcard} you can still see the form and use it)
You're trying to send a post request to a route expecting a get request.
Change:
{!! Form::open(['route'=>'fastsearch.show']) !!}
To:
{!! Form::open(['route'=>'fastsearch.index']) !!}
Where index is the name of the action you wish to receive the post request.
You're probably better off using specific named routes for this though.
Route::post('fastsearch', [
'as' => 'fastsearch.search', 'uses' => 'SearchController#search'
]);
Take a look at http://laravel.com/docs/5.1/controllers#restful-resource-controllers for more info on resource controllers and http://laravel.com/docs/5.1/routing#named-routes for more on named routes.
You can also use ./artisan route:list for a list of existing routes.
I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/