I got a form which sends many input fields to be validated into a controller in Laravel 5, and among them there is the files name="arquivos[]" <input> field. I want to solve all the validantion in a single step as I was doing but it seems that it is failing by the fact that it is receiving and array of files and not a single one. The code of the form is:
{!! Form::open(['route' => 'posts.store', 'enctype' => 'multipart/form-data']) !!}
...other inputs
{{ Form::file('arquivos[]', array('class' => 'novo-post-form-item', 'required'=>'','multiple' => '')) }}
{!! Form::close() !!}
And in my posts.store function:
$this->validate($request, array(
'arquivos' => 'required|mimes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
'assunto' => 'max:255',
'conteudo' => 'required|max:65535'
));
note: I don't know why in the validator I must specify the input name without the [] but it works that way...
This question is similar to some I found here at stack overflow but this time I'm asking if there is a solution for Laravel 5. As it seems, this validate methos is expecting a single file input field. Thanks in advance
Please read the Laravel documentation thoroughly. It's really helpful
To validate array field, you want this: https://laravel.com/docs/5.4/validation#validating-arrays.
Also I think you want to use mimetypes validation rule because mimes validation rule accepts file extensions as parameter: https://laravel.com/docs/5.4/validation#rule-mimetypes
And the solution to your problem:
$this->validate($request, array(
'arquivos.*' => 'required|mimetypes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
'assunto' => 'max:255',
'conteudo' => 'required|max:65535'
));
Related
In my blade I have this line of code:
{!! Form::file('motivation', old('motivation'), ['id' => 'inputGroupMotivation', 'class' => 'custom-file-input']) !!}
As you can see i set an id by doing 'id' => 'inputGroupMotivation'. However when I go to the page where this blade is rendered it outputs this:
<input name="motivation" type="file">
How come it does not take the id and class attributes I've set in my code?
I have not personally used the Form facade in Laravel, however from briefly looking at the documentation, it seems that the syntax for the file method is a bit different from regular input methods such as text. Namely, the second parameter is not the old value, but the list of attributes you would want to pass.
In your case you would this code instead:
{!! Form::file('motivation', ['id' => 'inputGroupMotivation', 'class' => 'custom-file-input']) !!}
For more information, take a look at the LaravelCollective's documentation page.
i tried these two methods..
first one : MethodNotAllowedHttpException
Route::post('/settings/{id}/update/', 'HomeController#update');
Route::match(['put','patch'], '/settings/{id}/update/','HomeController#update') use this also..
{!! Form::model($user, ['method' => 'patch','action' => ['HomeController#update',$user->id]]) !!}
another one
{!! Form::model($user, ['method' => 'patch','route' => ['user.update',$user->id]]) !!}
please explain how to use route for update default auth users.
You should give a name to the route:
Route::patch('/settings/{id}/update/', 'HomeController#update')->name('user.update');
Or:
Route::patch('/settings/{id}/update/', ['as' => 'user.update', 'uses' => 'HomeController#update']);
I think you should just be specific about the method you want to use, be it put or patch and also If i remember correctly, if you have to use patch method referencing the answer from this post: Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
The method field is required because as I understood, Laravel uses this mechanism to handle patch request.
PS: What I just tried to highlight if I understood correctly is that, there should be an extra field to handle the patch method.
Hope this helps :)
I am currently creating a website that, once the user arrives, they are greeted by a form with which they input their unique id and DoB. Upon entering the information and clicking submit, they are sent to the main form which has only a little information on it and the user must enter the rest. My problem arises when I try to submit the form as I keep getting the following error:
at RouteCollection->methodNotAllowed(array('POST', 'PATCH'))
Note: I do not want any variables in my routes. (ex: I want 'form/person' and not 'form/{person_id}'). Also, I have included only the relavent information regarding the errors.
gate.blade.php - (this is where the user enters their ID and date of birth):
{!! Form::open(array('action' => 'JurorsController#form', 'class' => 'form-inline')) !!}
form.blade.php - (this is the primary form the user must fill out and submit):
{!! Form::open(['url' => action('JurorsController#submit'), 'method' => 'PATCH', 'class' => 'form-inline']) !!}
routes.php:
Route::patch('jurors/form', 'JurorsController#submit');
Route::get('jurors', 'JurorsController#gate');
Route::post('jurors/form', 'JurorsController#form');
JurorController#submit
public function submit(FormSubmitRequest $request)
{
//never reaches this point nor executes submit... instead redirects to gate IF it doesn't return 'MethodNotAllowedHttpException' error.
dd($request);
}
The only time I managed to get it to not show me the 'MethodNotAllowedHttpException' exception, I instead got redirected to the gate.blade.php page. If you have any questions for me or need me to clerify on anything, leave me a comment and I will respond once I am able to.
Thanks.
It looks to me your problem is the url in our routes. You are repeating them.
Firstly I would recommend using named routes as it will give you a bit more definition between routes. I'd change your routes to
Route::put('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Route::get('jurors',[
'as' => 'jurors.gate',
'uses' => 'JurorsController#gate'
]);
Route::post('jurors/form', [
'as' => 'jurors.form',
'uses' => 'JurorsController#form'
]);
Also on your submit route why are you using a PATCH request. wouldn't you use a POST request with all the data in? If you do still need to use Patch then you should be using put instead in your routes.
Another way for for testing and debugging you could use any to see if it is your HTTP request which is causing the error for example
Route::any('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Also then you can use the name of your route in your form::open() for example
{!! Form::open(array('route' => 'jurors.form', 'class' => 'form-inline')) !!}
Hope this helps
Working on trying to get the following form's URL to populate properly. Been stumbling over this for some time so here for some help.
As you can see from the following code, I am opening the form - binding the model - and trying to set the URL dynamically. the full URL is something like {username}/account/cards/id so i need to pass it the username (which i would like to pass the authenticated user (as they would only have access to their own page) and the ID of the card they are trying to update.
{!! Form::model($card, ['method' => 'PATCH', 'action' => 'Account\CardsController#update', array(Auth::user()->username, $card->id) ]) !!}
Now this is all happening in blade (front end) so not 100% what i am doing wrong. I have tried action, url, route... I can not get anything to work for some reason. Error I am getting on this one specifically is a array to string error. But if I can't build an array how do i pass in multiple variables? So a bit confused here.
any help would be appreciated.
Thanks
Citti
This is an update to my previous answer. You can try this one:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', Auth::user()->username, $card->id] ]) !!}
You're just passing your route parameters as an option to the Form::model tag, not the route. Try:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', [Auth::user()->username, $card->id] ] ]) !!}
If you're still having trouble I would suggest you name your route and reference the named route in the action.
If I'm not mistaken, the action should be an array if you are passing variables through to the controller. Ex:
{!! Form::model($card, array('method' => 'PATCH', 'action' => array('Account\CardsController#update', Auth::user()->username, $card->id))) !!}
Not sure if you are using Larvel Collective HTML and Forms, but they are essentially the same as the Laravel version. This page: http://laravelcollective.com/docs/5.0/html#form-model-binding explains more about your particular use case.
Hope it helps.
P.S. Try adding:
{!! Form::hidden('_method', 'PATCH') !!}
...underneath the open tag instead of within it. This has to do with L5 method spoofing, and is generally necessary for anything that is not 'POST' or 'GET', I believe. (i.e., 'PUT', 'PATCH', and 'DELETE')
I have this code for form open :
{{ Form::model($user, array(
'route' => array('user_edit_put'),
'method' => 'PUT',
'role' => "form",
'class'=>'form',
'accept-charset' => 'utf-8'
)) }}
{{ Form::close() }}
but this always produces form with post method only, my question is how to create form with another http method in laravel ? why it's always give post method although I've set 'method' => 'PUT'?
If you look in the form there is a hidden field called _METHOD that will have the PUT method within it. This is to work around as form submission only supports GET and POST.
Here http://laravel.com/docs/html it says:
Note: Since HTML forms only support POST and GET, PUT and DELETE
methods will be spoofed by automatically adding a _method hidden field
to your form.