So i have a checkbox and i need certain values to be checkd i tried to do this and is not working
$columnas = [
1 => 'Columna 1',
2 => 'Columna 2',
3 => 'Columna 3',
4 => 'Columna 4',
5 => 'Columna 5',
6 => 'Columna 6',
7 => 'Columna 7'
];
$columnas_actuales = [
1 => 3,
2 => 5,
3 => 7,
];
<?= $this->Form->control('cliente_configuracion.columnas_visibles', [
'options' => $columnas,
'selected' => $columnas_actuales,
'multiple' => 'checkbox',
'label' => false,
'templates' => [
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxFormGroup' => '{{label}}',
'checkboxWrapper' => '<div class="form-check form-check-inline">{{label}}</div>',
]
])
$columnas_actuales is the array that contains the selected options but for some reason they are not being checked.
in new in cake and in php so i dont know if im doing something wrong.
Thanks !!
Edit: I added an example of the $columnas_actuales variable.
Your data looks correct, however there is no selected option. The options to pre-select values are called default and value, where the former can be overwritten by request/context data (for example when a user changes the selection and submits the form), and the latter will always fix the selected values.
So you'd probably want to use default:
'default' => $columnas_actuales,
See also
Cookbook > Views > Helpers > Form > Common Options For Specific Controls
Related
I am attempting to create several radio buttons with a specific label...
I am using the following code, however, there is no label
echo $this->Form->radio('qualify', [['value' => 1, 'text' => 'Yes'], ['value' => 0, 'text' => 'No']], ['label' => 'Qualify Only']);
Any ideas on how I can get the label to show up for these radio buttons?
<?php
$qualify = [['value'=>'0','text'=>'No'],['value'=>'1','text'=>'Yes']];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>
This code will be able to pick any existing value in the edit view.
You can still use the $this->Form->input and put a type whether its a select or radio
<?php
$qualify = ['0' => 'No', '1' => 'Yes'];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>
I have a form field in a view with a drop down selection.
echo $this->Form->input('form.selection', array('label' => 'Selection', 'type' => 'select', 'options' => $selection, 'empty' => '[None]));
The form field obtains its list of options from the $selection variable which is a nested array in order to help make the long list of options easier to read.
$selection = array(
'Section 1' => array(
'1' => 'Section 1',
'2' => 'Testing 1',
'3' => 'Testing 2',
'4' => 'Testing 3'
),
'Section 2' => array(
'5' => 'Testing 4',
'6' => 'Testing 5',
'7' => 'Testing 6',
'8' => 'Testing 7'
),
);
I am finding an issue where if one of the <option> texts matches the name of the <optgroup> then it is excluding it from the list which I do not want to happen.
Is there a suitable way to fix this without manually creating the <select> element and all the options inside?
Notes:
For the purpose of this question I have replaced the array of data as it contains potentially sensitive information which I do not wish to share and is only a selection of the full array.
Development Environment: CakePHP 2.8
I just looked up the source of FormHelper and found the following.
/* While a nested options array will create optgroups with options inside them.
* ```
* $options = array(
* 1 => 'bill',
* 'fred' => array(
* 2 => 'fred',
* 3 => 'fred jr.'
* )
* );
* $this->Form->select('Model.field', $options);
* ```
*
* In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
* attribute to show the fred option.
So you will need to set showParents to true in the select options. Look here: https://api.cakephp.org/2.8/class-FormHelper.html#_select
You'll find the answer here in the following link:
https://api.cakephp.org/2.8/class-FormHelper.html#_select
Therefore your select form field code should be as followed:
echo $this->Form->input('form.selection', array('label' => 'Selection', 'type' => 'select', 'options' => $selection, 'empty' => '[None]', 'showParents' => true));
showParents attribute should be added and value should be true. Hope this would be the answer.
$selected_country = array(1,3);
$this->Form->input('country', [
'options' => $source_types,
'label' => 'Country: ',
'multiple' => true,
'class' => ' form-control',
'selected' => $selected_country,
'type' => 'select'
]);
If selected country has only one value then it selects the option but if the selected country has more than one value then it doesn't select any value.
If you want to pass more than one value on $selected_country, try it:
echo $this->Form->select('rooms', [
'multiple' => true,
// options with values 1 and 3 will be selected as default
'default' => [1, 3]
]);
Reference: CakePHP Cookbook
For anyone this works
echo $this->Form->input('venues._ids', ['options' => $venues, 'class'=>'form-control select4', 'label' => false]); IF you have the database associations set up AND you have it referenced in the controller like so
$event = $this->Events->get($id, [
'contain' => ['Venues']
]);
otherwise adding the 'default' => [1, 2] ids in also works but it has to be first created in the controller and then fed to the view in list form.
I tried set checkbox value in false:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'test_checkbox',
'options' => array(
'label' => 'Test checkbox',
'use_hidden_element' => false,
'checked_value' => 1,
'unchecked_value' => 0,
),
'attributes' => array(
'value' => 0,
),
));
But as a result of this page contains:
<input type="checkbox" name="test_checkbox" value="1">
The value does not change and I can not understand why.
Other PHP and JS script not change this value.
Maybe I misunderstood how "checked_value", "unchecked_value" and "value" works?
The code you have is almost correct. It should be:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'test_checkbox',
'options' => array(
'label' => 'Test checkbox',
'use_hidden_element' => false,
'checked_value' => 1,
'unchecked_value' => 0,
)
));
But I think you misunderstand how HTML checkboxes work. The value attribute should always contain only the checked value. Browsers only submit this value if the checkbox is ticked. So when the page loads, the checkbox will correctly appear in the source as:
<input type="checkbox" name="test_checkbox" value="1">
To achieve the unchecked value, ZF (and all other frameworks I know of) add it to a hidden form field above the checkbox. If the checkbox is not ticked, the browser will submit the hidden form field instead.
Is it possible to include the option '0' in a dropdown generated by:-
'options' => Mage::helper('menu')->numberArray(5,Mage::helper('menu')->__('')),
This is part of...
$fieldset->addField('column', 'select', array(
'name' => 'column',
'label' => Mage::helper('menu')->__('Column'),
'title' => Mage::helper('menu')->__('Column'),
'required' => true,
'options' => Mage::helper('menu')->numberArray(5,Mage::helper('menu')->__('')),
));
Which generates a dropdown 1-5 but I'm wanting to include 0 at the beginning of this dropdown (or 'none' or anything other value that isn't an integer of 1+) but I'm unsure how to edit the above to allow this.
If anyone could advise what is needed please, that would be great.
Thanks in advance.
Since numberArray() is not a native Magento method, we can't know what this custom method really does in the end.
But, Varien_Data_Form_Element_Fieldset::addField() usually expects just a simple assoc array as value for the options key, so most probably something like this will work:
'options' => array(
0 => 'none',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five'
),