Laravel blade file input does not get ID attribute - php

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.

Related

Combining types of data to pass to #include

Below, I have the variable entryName available to use to insert data into my small-test blade view by using {{!! entryName !!}}.
#include('views.small-test', [
'entryName' => $data['entryName'],
])
But how can I append a static string to the front of it? Would using . work to concatenate it like below?
#include('views.small-test', [
'entryName' => "Welcome".$data['entryName'],
])
Yes, it works.
According to the laravel docs:
Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
#include('view.name', ['some' => 'data'])
You can also format the entryName on server side and send the variable to the view, or, within your small-test view, show the message like this:
Welcome, {!! $entryName !!}
Hope it helps.

How to validate mime for multiple files upload in laravel 5?

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'
));

Request name not being recognized for update method

Laravel is not returning the 'as' name of a specific request.
The below two examples show the output for each case (one works fine, the other does not)
The route is defined as a "resource", shows up in the route list as "companies.update" and all of the other routes work fine (except for update). Why is the update request not returning the route name?
{!! Form::model($company, ['route' => ['companies.update', $company->id], 'method' => 'patch', 'class' => 'form-horizontal']) !!}
#include('companies.form')
{!! Form::close() !!}
("update" does not return the name)
(every other route name works)
Have a look at php artisan route:list again.
There is two entries for update. One for PUT and one for PATCH.
Maybe use the method PUT to see if it shows up (but both should work when you look at the HTML).

Laravel 5 form URL creation

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')

Laravel 4 Populate Form fields with a Model but edit the field first

I am working with PHP and Laravel 4. Using the Form method to populate an edit form with a Model like below...
Form::model($timecard, array('route' => array("admin/timecard/edit", $id)))
My problem is, some of the text fields get populated with DateTime values from the Database and I need to be able to run some code on these certain fields before it populates the Form field.
Any ideas how to do that or if it's possible to do that while still using the Model to auto-fill the Form fields?
For example this form field below gets filed with a GET value, otherwise it gets field with the Data from the Database for column clock_in_datetime however I would like to run a PHP function on this field before it fills the form so that I can apply TimeZone or other formatting to it...
{{ Form::text("clock_in_datetime", Input::get("clock_in_datetime"), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
I believe you can do the following:
{{ Form::text("clock_in_datetime", yourFormattingFunction(Form::getValueAttribute("clock_in_datetime")), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
Form::getValueAttribute() is Laravel's way of deciding which value to use (previous Input, Session or Model). So you can apply your formatting function to the output of this function.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#751-773

Categories