Hi I'm using Laravel version 5.6 and got a problem in view with Form::select.
I already made some Form::open with 'text' and 'textarea' they all worked fine, but the Form::select do not generate the select fild on my view.
I used this code:
{!! Form::label('isPropaganda', 'Propaganda:') !!}
{!! Form::select('isPropaganda', ['Não' => '0', 'Sim' => '1'], null, ['class'=>'form-control','multiple']) !!}
I found the example here http://laravel-recipes.com/recipes/163/creating-a-select-box-field but didn't workout. How can I fix it?
Try flipping the keys and values. Keys cannot have special characters in them. Also, I'm not sure what that 'multiple' attribute is doing there. If it's meant to be a css class, place it inside the 'class' array key, otherwise add it to a new attributes array.
{!! Form::select('isPropaganda', ['0' => 'Não', '1' => 'Sim'], null, ['class'=>'form-control multiple']) !!}
Related
I need to have a prompt box which would be set through the key 'onsubmit' in Form array. This version works:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\'Are you sure?\')']) !!}
But I am unable to inject a translation into it to make it work. So far I tried this without success:
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => 'return confirm(\''.{{trans('Users::users.current.account.index.box.confirm')}}.'\')']) !!}
Is there a way to concatenate function output inside the array value?
Please Try
{!! Form::model($currentUser,
['route' => ['post.users.current.account.index', $currentUser->id],
'onsubmit' => "return confirm('".trans('Users::users.current.account.index.box.confirm')."')"]) !!}
{!! Form::select('country',['1' => 'Albania','2'=>'Kosovo','3'=>'Germany','4'=>'France'],null, ['class'=>'form-control','placeholder'=>'Select Country']) !!}
This type of input doesn't take the values of the array but instead, it takes the pre-default value of option inside select.enter code here
According to the Form Collective documentation, the first argument of the Form::select() is the name of the select box, the second is an array of the input values while the third is the default value (this could be set to null). The fourth is an optional array of attributes/values.
This should work for you.
{!! Form::select('country',['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'],'Kosovo',['class'=>'form-control','placeholder'=>'Select Country']) !!}
{!! Form::select('country', [null => 'Select Country'] + ['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'], null, ['class' => 'form-control']) !!}
For more information : https://laravel.com/docs/4.2/html#drop-down-lists
I am using form builder and I share one form for edit and crete page so there are some variables I don't want to use.
I know that:
{!! Form::text('name', isset($admin_link->name) ? $admin_link->name : null, ['class' => 'form-control']) !!}
would work but I do not consider it the best way. Is there any way to use something else like isset for form builder?
I sent the model to the blade with values what caused autocomplete of the input what is probably what I have been looking for:
{!! Form::model( $admin_link, ['route' => ['admin.links.update', $admin_link->id], 'method' => 'post', 'class' => 'main_link_form']) !!}
I have a form and if i submit the form with all the right data everything goes perfectly fine... but if I intentionally make any flaw
(validation for example 'title' => 'required|min:2')
and I put only one character for title or if I miss any required field I get this error:
htmlspecialchars() expects parameter 1 to be string, array given
I have figured out that the problem is with this select box
{!! Form::select('item[0][]', $items, null, ['class' => 'form-control', 'required']) !!}
and I even tried to use a normal select box without form helper {!! !!}
But I still get the same error!
So the problem is somewhere with validation when there is a nested array....is there a way to fix this?
OK I finally have an answer for this problem....it seems like something has changed in Laravel 5.3 and if you want to have a name with array like this
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required') !!}
You must put [0] something in brackets 'indices' like this:
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required') !!}
and then in validation use
title.*
for rule
UPDATE
Because i use dynamic form that can be expanded and new form fields added (optionally) i needed to put [] array notation for a name but actually if you already have hard coded many fields with the same name like item[]
you don't have to put [0] indices inside. The validation will work for them.
The problem comes only if you have a single input field and you put [] array notation along the name for example 'item[]'
this will trigger the error if any validation rule is broken...
I want to pass get all existing locales to view. This is my code
view
{!! Form::select('language', $languages,null, ['placeholder' => 'Pick a language']) !!}
controller
this only pull the current how can I pull all with eloquent
$languageCurrent = App::getLocale();
How can I pass it into view(when I'm manipulating data from database I can return with something like this)
->with('users', $users)
How can I return value as array
If you have multiple locales defined in config/app.php, like described here:
'locales' => ['en' => 'English', 'sv' => 'Swedish'],
You could try to do this:
{!! Form::select('language', array_flip(config('app.locales')), null, ['placeholder' => 'Pick a language']) !!}
config() will get locales list and array_flip() will swap keys and values for Form::select.
You can add an array in /config/app.php containing the locales which you use, for example : 'locales' => ['en' => 'English', 'pl' => 'Polish'] than you should be able to use config() helper function to get the values like $available_locales=config('app.locales');