How do I set a certain option as currently selected?
My current code is:
{{ Form::select('make',$makes , NULL, ['class' => 'form-control']) }}
$makes is an array of car makes.
Lets say the array looks like:
[
0 => 'Audi',
1 => 'BMW',
2 => 'Mercedes'
]
If I am editing a car, I know that in my DB I have said that car is X make.
In this case, lets say its a BMW. When I create this select input I want BMW to already be shown as the selected option. I have this value already, its just getting the blade input to set it to selected.
I tried:
[$tMake->display_name] + $makes
But that just add a new option to the list, which is not what I want.
Thanks.
You need to tell the select the ID you want pre-selected.
You can do this:
{{ Form::select('make',$makes , $tMake->id, ['class' => 'form-control']) }}
And if you are using validation - you'll want to do something like this, so that the old input is shown if the validation fails
{{ Form::select('make',$makes , Input::old('make', $tMake->id), ['class' => 'form-control']) }}
Related
I have the following form field in a Laravel project in my create view (create.blade.php):
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), null, ['class' => 'form-control format']) }}
I retrieve this inside the format column in my database so i can use this data in my project.
Now, inside the edit (edit.blade.php) view I want to have the already selected data to reflect. So when the user has selected "video", the select option will already be set to Video.
While I was typing this question, i figured it out. Already typed the entire question so might as well post it to help someone out.
The third argument needs to correspond with the data I retrieve from the database. So, for example, if i wanted the Video to be selected, it would look like this:
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), 'is_video', ['class' => 'form-control format']) }}
I replaced the is_video with the variable that contains the data from my database and it's working as expected.
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), $var->format, ['class' => 'form-control format']) }}
{!! 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 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 am using laravel form to insert multiple colors using from input type as color,
{{ Form::input('color','color[]',null, array('class' => 'form-control-color','placeholder' => 'Enter Color','id' => 'exampleInputTitle1')) }}
But i am getting this error, "htmlentities() expects parameter 1 to be string, array given", Can any one have an idea to how to handle the Color input field as multiple values.
I am unsure of this, but if you try:
{{ Form::input('color','color[0]',null, array('class' => 'form-control-color','placeholder' => 'Enter Color','id' => 'exampleInputTitle1')) }}
Does it work?
I have a serialized data saved in my database, it's a values from array of categories. I created this because it's easy to manage what categories are selected.
So for creating a "page" i created create.blade.php page and form for multi select categories.
{{ Form::open(['role' => 'form', 'method' => 'post', 'route' => 'admin.games.store', 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
I defined $developer, $genre and $platform in my controller, this is basically a list of categories under specific id.
$platform = Category::->where('type', '=', 1)->lists('name', 'id');
So this returns an array of all categories for a view.
----------------
| name | id |
----------------
| Windows | 1 |
----------------
| Linux | 2 |
----------------
| MacOS | 3 |
----------------
So this all works fine i have my select forms working fine with categories i specified and upon submit i get an array of values, as array can't be saved i serialized that very simple with:
serialize(Input::get('categories')
And i have serialized data in my database, i know someone would say it's not a good way if i need to search etc... i will not be able to use that field. But for me i need only to store id's of selected categories and query them all together at once. so this is good for me.
But now i have a problem, i am not finding a way to get that data when i edit a page.
I use Route::controller so you know work flow how the pages are updated, stored, edited, created... And since form can automatically populate the fields using Form::model i used that before and it's very nice feature. But i don't know how to populate the fields now when i edit the page.
Here is a form on my edit.blade.php
{{ Form::model($game, ['role' => 'form', 'method' => 'PUT', 'route' => ['admin.games.update', $game->id ], 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
So how do i get that serialized data and populate the select fields with selected categories when editing a page using Form::model. Any ideas, i searched on net and no answers.
I think this should tell you what you need to know: Getting selected values from a multiple select form in Laravel
So instead of passing in a null third parameter you instead pass in an array of selected values.