Getting always last value when trying to create a radio button using loop in cakephp - cakephp

I'm trying to create a radio button using a loop
<?= $this->Form->create($account) ?>
<?php foreach($gametitles as $gametitle): ?>
<?= $this->Form->radio(
'gametitle_id',
[
['value' => $gametitle->id,'text'=>'','hiddenField' => false],
]
);
?>
<?php endforeach; ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I have added 'hiddenField' => false , but in DOM I'm seeing still hidden field displaying. In output I'm getting always empty value if I select without last radio button.
[
'gametitle_id' => '',
]
If I use name as an array gametitle_id[] I am getting an array
'gametitle_id' => [
(int) 0 => '',
(int) 1 => '',
(int) 2 => '4',
(int) 3 => '',
(int) 4 => '',
],
How I will get only one value that has selected in radio button ? I have used 'hiddenField' => false, still why hidden field is displaying ?

You have set 'hiddenField' => false in options ! You have to set it with name.
echo $this->Form->radio(
'favorite_color',
[
['value' => 'r', 'text' => 'Red', 'style' => 'color:red;']
],
['hiddenField' => false]
)

Related

CakePHP2 - Default value for input - select with option multiple

I have Form input with multiple select options. I am unable to set default values. This is my code:
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => array(
'cash'=>'cash',
'invoice'=>'invoice',
'ax'=>'ax',
'ca'=>'ca',
'vi'=>'vi',
'tp'=>'tp',
'dc'=>'dc'
),
'default'=>'ax'
)); ?>
How do I set default values for this input with PHP only?
This is working on my system. You can also set it from controller like this :
$this->request->data[$this->modelClass]['PaymentMethods'] = 'ax';
Please check these url also
CakePHP select default value in SELECT input
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
example :
$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
'size',
array('options' => $sizes, 'default' => 'm')
);
Since this is multi-choice select, value given must be array. And the key shouldn't be default, I should've used value instead.
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => $options,
'value'=> $array_of_data_fetched_from_database
)); ?>

How to display boolean value(tinyInt 1) in Form Select so that it's value is autoselected in cakephp3?

I have tinyint(1) column named status in my mysql table. I am displaying this field in Form as select input. My problem is form select value is not autoselected , first option is always selected ,however if i use checkbox instead of select input , value is correctly autoselected. How can i make value autoselected in select input for boolean column in cakephp3? Below are the codes i tried which doesn't work.
<?php
echo $this->Form->select('status',
[1 => __('Active'), 0 => __('Inactive')],
['class' => 'form-control input-medium']
);
?>
<?php
echo $this->Form->select('status',
[true => __('Active'), false => __('Inactive')],
['class' => 'form-control input-medium']
);
?>
this works for me
$this->Form->input('status', [
'options' => [
0 => __('Inactive')
1 => __('Active')
],
'class' => 'form-control input-medium'
]);

How to mark checkbox as selected using Bitwise in CakePHP?

I am developing an application in CakePHP 2.6 and I have a form where a user can set a series of flags when creating a calendar event.
I have managed to set up the 'add' action to display the flags and to also loop through in the controller after validation and save the value into my table. This process is done using bitwise. Code example below:
'add' action view:
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false));
'add' action controller:
$flags = 0;
foreach ($data['flag'] as $r) {
$flags |= (int)$r;
}
I am however having trouble getting the checkboxes for the flags to be marked as selected in the edit action view when they are displayed.
'edit' action view:
$selected = array($results[0]['BitwiseFlag']);
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false, 'selected' => $selected));
$results[0]['BitwiseFlag'] = 32 in the table.
$flagtypes array:
array(2) { [32]=> string(4) "Test" [64]=> string(9) "Testing 2" }
Not fully sure what your forms data structure looks like, but you should be able to set the flags from within your controller. Set the relevant values in $this->request->data, something like this:-
$this->request->data[$this->{$this->modelClass}->alias]['flag'] = [
0 => true,
1 => false,
2 => true
];
Then when you use $this->Form->input('flag', [...]) it should check the correct flags for you.
Change your input line to this :
$selected = array($results[0]['BitwiseFlag']);
// Change 'checked' to 'selected'.
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false, 'selected' => $selected));

CakePHP Form Action Button

I am trying to place a non form related button into the view for a registration button to direct people to a controller action but i continue to get error500 internal error. Any ideas what i am doing wrong here?
<?php
echo $this->Form->create('User');
echo $this->Session->flash();
echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $form->button('Register', array('type' => 'button', 'class' => 'button red tiny'));
echo $this->Form->submit('Login', array('class' => 'button blue tiny'));
echo $this->Form->end();
?>
$form->button is CakePHP 1.2 syntax, $this->Form is 1.3 onwards.

cakephp: How to set checkbox to checked?

I am using
$form->input('Model.name', array('multiple'=>'checkbox');
I am trying to base on model data to set certain checkboxes to checked.
How can i do that?
cmptrgeekken's solution works for a single checkbox. I'm assuming you're generating a multiple checkboxes, for a HABTM relation or something similar.
You need to pass a array with the values of the elements that are going to be selected to the method, like this:
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected));
is going to generate this:
<div class="input select">
<label for="ModelName">Name</label>
<input name="data[Model][name]" value="" type="hidden">
<div class="checkbox">
<input name="data[Model][name][]" checked="checked" value="1" id="ModelName1" type="checkbox">
<label for="ModelName1" class="selected">ONE</label>
</div>
<div class="checkbox">
<input name="data[Model][name][]" value="2" id="ModelName2" type="checkbox">
<label for="ModelName2">TWO</label>
</div>
<div class="checkbox">
<input name="data[Model][name][]" checked="checked" value="3" id="ModelName3" type="checkbox">
<label for="ModelName3" class="selected">THREE</label>
</div>
</div>
The first and third checkbox checked.
Just remember that you're actually working with a multiple select element that is just displayed as a bunch of checkboxes (Which is IMO better because of the usability).
I don't use CakePHP, but according to the docs, it appears as though you should be able to add the option 'checked'=>true:
$form->input('Model.name', array('type'=>'checkbox','checked'=>true));
since that's one of the options of the checkbox function.
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name',
array(
"name"=>$mnus['Aco']['id'],
"type"=>"select",
"multiple"=>"checkbox",
'options' => $options,
'selected' => $selected)
);
this is the correct way for multiple check box and checked option. I am using this in cake1.3 please recheck once on your code it must work.
echo $this->Form->input('Title', array('type'=>'checkbox', 'label'=>'Label', 'checked'=>'checked'));
The Marko solution still working in CakePHP 2.0+
-> https://stackoverflow.com/a/1962499/3197383
It just need to correct with the new syntax :
<?php
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $this->Form->input('ModelName',
array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected)
);
?>
Its Super Simple
$form->input('field_name', array('type'=>'checkbox','checked'=>true));
That's it.
Documentation: https://book.cakephp.org/3.0/en/views/helpers/form.html
Another way to have a checkbox checked with the "label" right next to it is.
$form->checkbox('Model.name', array('checked'=>'checked'))?> Label
Label can be what ever you want though. example: 21,000-3000, Tire, Human. I am sure you get the idea.
<?php
$subjects = array(1=>'Snow boarding',2=>'Surfing',3=>'Trekking',4=>'Swimming');
$selected_skills = array(0=>2,1=>4);
// For MutiSelect box with selected
$form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>true,'selected'=> $selected_skills));
//For Multiple checkbox with checked
$form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>'checkbox','selected'=> $selected_skills));
?>
Here is a small code snippet from one of my project-
$categories = $this->Site->Category->find('list', array('recursive' => -1));
$this->set(compact('categories'));
$this->Site->Category->bindModel(array('hasOne' => array('CategoriesSite')));
$selected = $this->Site->Category->find('list', array(
'fields' => array('id'),
'conditions' => array(
'CategoriesSite.site_id' => $this->data['Site']['id'],
),
'recursive' => 0,
));
$this->set(compact('selected'));
Main key is for selected is 'fields' => array('id')
$options = array("fixed","varry");
$selected = "0";
echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'value' => $selected));
Use the value attribute to make checked default.
'likes_preferences' =>array(
'type'=>'select','label' => 'Main likes/preferences',
'options' => $this->Ethos->toOptionsArray('preferences'),
'multiple' => 'checkbox',
'div'=>array('class'=>'input select checkbox-group clearfix'),
'hiddenField' => false,
),
the above code for adding the data, you need to change the field 'likes_preferences' from array to comma separated string before saving into database.
$preferences = implode(',',$this->request->data['Member']['likes_preferences']);
$this->request->data['Member']['likes_preferences'] = $preferences;
EDIT MODE
$likes = explode(',',$this->request->data['Member']['likes_preferences']);
'likes_preferences' =>array(
'type'=>'select','label' => 'Main likes/preferences',
'options' => $this->Ethos->toOptionsArray('preferences'),
'multiple' => 'checkbox',
'div'=>array('class'=>'input select checkbox-group clearfix'),
'hiddenField' => false,
'selected' => $likes
),
you are done, again you must convert the array to string while updating the database in edit action.
You can also try this for input with single option
$this->Form->input('name', array('type' => 'checkbox', 'default' => 1, 'checked' => 'checked'));

Resources