I am trying to create a drop down menu (option) and to fill this drop down i have sent an array list to the view:
$country = $this->country_list;
$this->set(compact('country'));
Now my question is does cake have a buildin method for me to set an input field using ($this->Form->input()) with the data of the array list?
take this example
$sizes = array(
's' => 'Small',
'm' => 'Medium',
'l' => 'Large'
);
echo $this->Form->input('size', array('options' => $sizes, 'default' => 'm'));
In the controller, set the value
$this->set('countries', $this->Country->find('list', array('fields' => 'Country.name')));
To show the dropdown box in the view
$this->Form->input('country_id');
Related
Form:select()
I want to select multiple options in select, but its only work if Array index key is string, If array index key is numeric its not working...
Any help please..
Working Example:
Form::select($field_data['name'],
array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), //Options list
array('S', 'M'), //Selected values
array('multiple')); //Multiple True
//Result: Form print and Large and Small selected
Not working with Numeric Array Keys
Form::select($field_data['name'],
array('5' => 'Large', '2' => 'Medium', '10' => 'Small'), //Options list
array('10', '2'), //Selected values
array('multiple')); //Multiple True
// Just Form>select>options print, but no option selected
I want to select multiple option, and options keys are numeric ids..
Try changing it to integers.
Form::select($field_data['name'],
array(5 => 'Large', 2 => 'Medium', 10 => 'Small'), //Options list
array(10, 2), //Selected values
array('multiple')); //Multiple True
You can trying like this:
$selected = array('10', '2'); //Selected values
Form::select('sections[]', ['5' => 'Large', '2' => 'Medium', '10' => 'Small'], $selected, ['multiple']);
Its actually a bug which PR has already been open.
https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423
Pluck id (integer) cast to string Laravel
Hi I am trying to disable one radio button value at multioption element
$elements[] = $em = $this->createElement("radio", "tshirt-$member",array(
"separator" => " ",
"belongsTo" => "tshirt",
"class" => "flow",
"parentClass" => 'renew-donation-holder',
"label_class" => 'span6',
"multioptions" => $options['options'],
'value' => ct_get($this->memSession, "tshirt.tshirt$member", $entry->athlete->tshirt_size),
));
try to disable it with radio button value
$em->setAttrib('disable', array( 'L'));
or to add
"disable" => array("L")
Is there any solution for this, another option is to add data atribute as some kind of flag?
I believe you are on the right track. Keep in mind that using this method you should specify the key of the associative array, not the value, depending on what your $options['options'] are.
Example:
$element = $this->createElement('radio', "tshirt-{$member}", [
'multiOptions' => [
's' => 'S',
'm' => 'M',
'l' => 'L',
]
]);
Disabling one (or more) items should now be:
// Single
$element->setAttrib('disable', ['s']);
// Multiple
$element->setAttrib('disable', ['s', 'l']);
I want to choose the type of my calendar events from some predefined values but also, create a new (custom) type if it's not listed.
So i have created the field in $db like so:
'Type' => 'Varchar',
'EventCustomType' => 'Varchar'
Then, in the getCMSFields() i have:
$f->addFieldsToTab("Root.Main", $eventType = new OptionsetField(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => TextField::create('EventCustomType','Event type')
)
)
);
The problem is that i don't know how to insert the label "Custom" before the Textareafield and style them in the same line.
Also, i'm not sure if i need a second field for the custom one. Can i insert the custom value inside "Type" field or validate it ?
Thanks for any suggestions
This could be achieved by having a separate field for "EventCustomType" and then using Display Logic to show it with something like...
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$fEventCustomType = TextField::create('EventCustomType','Event type')
->displayIf('Type')->isEqualTo('custom');
$f->addFieldsToTab("Root.Main", array($eventType,$fEventCustomType));
As an alternative if you wanted to rescue This module then you could create this to save into one field as it is designed to do as you are asking... but it is with an error (last time I tried) so it is refernce only for now.
Finally, i have figured it out with separate fields:
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$customEventType = TextField::create('EventCustomType','Custom type');
$f->addFieldsToTab("Root.Main", array($eventType,$customEventType));
and jQuery:
$('#Root_Main').entwine({
onmatch: function(){
var $tab = this.closest('.tab');
var $eventType = $tab.find('ul.eventType');
var $customType = $tab.find('.customEventType').hide();
if($eventType.find('input:checked').val() == 'custom'){
$customType.show();
}
$eventType.find('input').change(function(){
if($(this).val() == 'custom'){
$customType.show();
}else{
$customType.hide();
}
});
}
});
I have a model that has fields id_peg, nama_peg, and jabatan_peg. So here's what I wanna do:
Autosuggestion that shows the list ofid_peg as I type in the text field id_peg
When I select the suggested id_peg, the fields nama_peg and jabatan_peg are autocompleted based on that corresponding selected value (retrieved from database)
I've been trying to do the first one and I did it. But I'm stuck at the second one.
This is what I did:
view/melaporkan.php:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test1',
'source' => $this->createUrl('search'),
// additional javascript options for the autocomplete plugin
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui) {
$('#nama_peg').val(ui.item.nama_peg);
$('#jabatan_peg').val(ui.item.jabatan_peg);
}',
),
));
controllers/sitecontroller.php
public function actionSearch() {
$res = array(0 => array('id' => 1, 'value' => "id_peg"), 1 => array('id' => 2, 'value' => "jabatan_peg"), 2 => array('id' => 2, 'value' => "nama_peg"));
if (isset($_GET['term'])) {
$qtxt = "SELECT id_peg FROM pegawai WHERE id_peg LIKE :id_peg";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":id_peg", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
$res = $command->queryColumn();
}
echo CJSON::encode($res);
Yii::app()->end();
}
All it did was autosuggest for id_peg. When clicked some random id_peg, nama_peg and jabatan_peg were still empty.
What did I do wrong?
Actually the problem is controllers/sitecontroller.php page.
You are sending only id_peg as JSON format but trying to get as ui.item.jabatan_peg.
It is always better to send your value as
[{"id":"Caprimulgus europaeus","value":"European Nightjar"}] pattern
and get the value ui.item.value at your select function.
I have a form having some multi select dropdowns in yii. I have to auto populate the selected values in that multi select box when comes to edit the data. I am fetching the data and passing it inot view file, but am not sure how can i populate the values in the dropdown. please help me
Here is the code for the multi select drop down
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;'));
For multiple select you need to use 'options' parameter:
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;', 'options' => array(
1 => array('selected' => 'selected'),
2 => array('selected' => 'selected'),
)));
Where 1 and 2 are domain ids taken from the $_POST;
You can do this in the action:
$post = $this->getRequest()->getPost('ModelName');
$selectedDomains = $post['domain_id']; // this should be an array or selected values
$selected = array_fill(0, count($selectedDomains), array('selected' => 'selected')); // this constructs the 'options' value from view
$selectedOptions = array_combine($selectedDomains, $selected) // this is the 'options' array with selected values as keys and htmlOptions as values
Also, the code is untested and you need to do your validations and other logic stuff yourself.