Select input with Laravel Collective - php

{!! 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

Related

Laravel Form::select shows nothing

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']) !!}

isset for form builder

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']) !!}

htmlspecialchars() expects parameter 1 to be string, array given

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...

Drop down select form in Laravel

may I know what is wrong with my codes? I have three user type name registered in my database however my codes will result to three drop down menus with individual user type name on each.
#foreach($user_types as $usertype)
<div class="form-group">
{!! Form::select('chap_user_type_name', array('chap_user_name' => $usertype), null, ['class' => 'form-control']) !!}
</div>
#endforeach
remove foreach and pass array into select like this
{!! Form::select('chap_user_type_name', $user_types, null, ['class' => 'form-control']) !!}
If $user_types is a collection, you need to use pluck() to build correct array for ::select:
$user_types = UserTypes::pluck('name', 'id');
Then just build select element like this:
{!! Form::select('chap_user_type_name', $usertype, null, ['class' => 'form-control']) !!}
You don't need the foreach above form::select will take care of it

Laravel returns htmlentities() expects parameter 1 to be string, object given

I have the following Laravel 5.1 controller function
public function editare($prod_id) {
$categorii=DB::table('categorii_produse')
->select('cat_id')
->get();
$categorie_selectata=DB::table('produse')
->leftjoin('categorii_produse','prod_cat_id','=','cat_id')
->where('prod_id','=',$prod_id)
->select('prod_cat_id')
->get();
$articole=DB::table('produse')
->leftjoin('imagini','prod_id','=','img_prod_id')
->where('prod_id','=',$prod_id)
->get();
return view ('pagini.editare',compact('categorii','categorie_selectata','articole'));
And the following line in the view which has problems
{!! Form::select('categorii',$categorii, null, ['class' => 'form-control']) !!}
The view returns the following error
htmlentities() expects parameter 1 to be string, object given
As 2nd argument you need to pass array in format value => displayed option (in your case I see you only use cat_id as both value and displayed option), so instead of:
{!! Form::select('categorii',$categorii, null, ['class' => 'form-control']) !!}
you should use:
{!! Form::select('categorii',collect($categorii)->lists('cat_id')->all(), null, ['class' => 'form-control']) !!}

Categories