I've got a string value from a controller which is:
$pickedValues = "1,2";
So what I'm trying to do is set the multiple selected values on the view, here is the select code that I have right now:
{{Form::select("selection[]", $data, (isset($pickedValues)) ? array($pickedValues) : '',
[
"class" => "multiple-select",
"multiple",
])
}}
That problem that I'm getting right now that it is not displaying the selected options, but if I put the values manually like array(1,2) it works. I've tried setting it as array((int)$pickedValues) but it only displays the value 1.
I found a solution to this problem,
Try the value like this
array('1','2')
Related
I have this issue:
in Controller:
$countries=mytable::lists('country');
return view('myview', ['countries'=>$countries]);
In view:
{!!Form::select('Country', $countries, $item->Country)!!}
$item->Country is the country of the user and it should be the default option already selected. However, what I get is the list of countries in alphabetical order... as it comes from the database.
My question is what am I doing wrong? Why the select tag doesn't get the default option ?
Thanks.
Try this:
{!! Form::select('Country', $countries, $item->Country, []) !!}
The select input is looking for 4 parameters
The name of the input
The values
The selected value
And an array of other attributes (class, id etc)
What is more, I hope you know, that this only gives you an array of the country names.
$countries = mytable::lists('country')
$item->Country probably gives you a name of the country, whereas the select input has values of 0, 1, 2, 3 and the texts are the names of the countries. So there there isn't a value of a country's name in the select input.
You should edit your question and add the description of mytable so that we'd know how to help you.
As i think, $item->country is an object, you shoud set the id itself
{!!Form::select('Country', $countries, $item->Country->id)!!}
Also in your controller, add id as a key to the collection:
$countries=mytable::lists('country', 'id');
My project is about shops belongs to many sections.
When creating a shop, sections are chosen (selected) in a multi-select list and values are saved correctly. Also when editing the info, user can save changes correctly. The problem is that: in edit form multi-select object is clear (no items are selected at all) while we need to set options to be selected according to the pre-saved data, so user can see and modify it.
in controller I select lists like this:
public function edit($id)
{
$data = Shops::with('sections')->where('id', '=', $id)->get();
$sectionslst = Sections::lists('section_name','id');
return View::make('admin.shops.edit')->with('data',$data)
->with('sectionslst', $sectionslst );
}
and I populate data in my view this way
{{Form::label('shop_section','الاقسام') }}
{{ Form::select('shop_sections[]', $sectionslst, array (1, 2), array ('multiple' => 'multiple' ))}}
//I'm using array(1, 2) just to try how it works, but it doesn't, all items still un-selected :-(
First, you obviously can't use array(1,2) to test it if you don't have those id's in your list. Instead:
{{ Form::select('shop_sections[]', $sectionslst, array(13, 14), array ('multiple' => 'multiple' ))}}
Now the real problem here is that $data->sections is an Eloquent collection of models. However Form::select expects an array of keys. You can fix that by using lists()
$data->sections->lists('id')
I am using Yii and I have a dropdown using the following example:
$form->dropDownList($model,'sex',array('1'=>'men','2'=>'women'), array('options' => array('2'=>array('selected'=>true))));
Here I am able to choose which option is selected. If I set two as shown in the example above the selected option is women as expected.
I am not able to statically set the selected option as I need to use a variable. I have $selectedId which equal 2, but when doing for example:
array('options' => array("$selectedId"=>array('selected'=>true))));
or doing like this:
array('options' => array($selectedId=>array('selected'=>true))));
I am getting no errors, but the dropdown does not have the expected selected option. Is it possible to use a variable when defining an array key?
Update
True string:
CHtml::dropDownList('package','',CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))
You can set it by setting the second parameter:
CHtml::dropDownList('package',$selectedId,CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))
I just discorvered that Form::model binding existed and I'm delighted with (it's wonderful). I tried it with text, email, and even select, every single time it worked.
My question is, will it work with a <select multiple>? If it does, how am I supposed to use it and what is the correct way to save an array in the database? (This may be awful but I concatenate all the options of the array with a separator and save it as text, I'm sure it's not the correct way to do it).
just like this :
Form::select('menus[]', $menus, null, array(
'multiple' => true,
'class' => 'form-control'
));
take a note :
param 1 : should be your field name (if want multiple add array tag aftrer field name e.g :menus[])
param 2 : list menu (array) e.g : array('value1' => 'text1', 'value2' => 'text2')
param 3 : selected values. (should be null because Form::model will perform automatically matching values from database. and make sure the field name has same with key data result from database)
param 4 : is property for element <select> you can add class, id, and etc..
I know this has been asked before, but I just cant seem to find an answer... or solution.
I have many select boxes using 'multiselect'. The dropdowns are being populated from the database, and the first value in the array is always 'Select One'. This I cannot change, I am rewriting an app and am not changing the database at all.
Everything works just fine, but they always come out as 'optgroup' tags with a label, which always puts a '0' at the top of the list. The boxes always say 'Select One', which is great, but when expanded you see the '0' at the top... which is the 'label' attribute to the optgroup tag.
I do it all somehting like this...
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'Progress Notes: Program Status' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$ProgramStatuses = DictionaryPeer::doSelect($Criteria);
$ProgramStatusList = array();
foreach ($ProgramStatuses as $ProgramStatus) {
$ProgramStatusList [ $ProgramStatus->getDictionaryID() ] = $ProgramStatus->getWord();
}
$form->programstatus->addMultiOptions( array(
$ProgramStatusList ));
echo $form->programstatus->renderLabel() . $form->programstatus->renderViewHelper();
I just want to remove the '0' for presentation purposes only...
Any help is always appreciated...
Thanks!
If you want to get rid of the OPTGROUP, you just need to pass a simple array as parameter to addMultiOptions() as follows:
$form->programstatus->addMultiOptions($ProgramStatusList);
Because if you pass a multidimensional array, Zend_Form will indirectly consider each index of the parent array as a group of options (using the FormElement View Helper).