Checkbox in Form Helper Cake PHP - php

Here is the form helper I have used for checkbox
<?php
echo $this->Form->input('name',array('type'=>'checkbox','options'=>$options));
?>
and $options array is as follows:
[options] => Array
(
[58] => 58
[85] => 85
)
But I am getting only one check box with both values in it. How can I get check box for each values.

Use the multiple attribute.
echo $this->Form->input('Name',array(
'label' => __('Label',true),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
));

Another thing you have to check, and this is truly a general rule in cakephp when things do not run as expected. is:
"Are you properly closing the form? Do your inputs stay inside <form>...</form>? If you are not sure how to check simply use your preferred
DevTool and check the rendered HTML page.
This is almost the thing I forgot to check mostly and which always let me waste a lot of time!

If your are creating the $option variable in the view this will help you :
$options = array("key" => "value" , "key" => "value" , "key" => "value");
But if you are setting it the controller this will help you :
$this->set('options', array("key" => "value" , "key" => "value" , "key" => "value"));
key is the value in each option of the select input
value is the text of the option tags

Related

CakePHP multiple checkbox array HTML the right way

CakePHP's form generator for checkboxes ... when passing the following into the name:
<?php echo $this->Form->checkbox('checkList[]', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>
outputs:
<input type="checkbox" name="data[HolidaysApproval][checkList[]]" value="1" id="holiday238" class="approveHolidayCheckbox" data-off-text="No" data-on-text="Yes">
I read here:http://network-13.com/thread/3647-Creating-checkbox-arrays-with-CakePHP that the solution is adding a full stop to the field name (as below), where multiple checkboxes are output on the page. Is this the 'right' way to do this?
Couldn't see this particular scenario anywhere in the documentation.
<?php echo $this->Form->checkbox('checkList.', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>
Yes this is the correct way of doing this. When CakePHP builds the field names it uses PHP's explode() method. So checklist. essentially does the following:-
$fieldname = explode('.', 'checklist.');
Which results in:-
Array
(
[0] => checkList
[1] =>
)
So you would get inputs with the name data[Model][checklist][].
You can similarly use this for hasMany like fields, e.g. $this->Form->field('Model..name'); which would give you inputs with the name data[Model][][name].
Take a look at the FormHelper and you should easily be able to see how it builds the field names.

How to add checkbox to stwe/DatatablesBundle for symfony2

I am using https://github.com/stwe/DatatablesBundle for my symfony2 application. It's still buggy but works great. Now I want in my table I have id, name for the id. I want to add check box so that I can select them by their id. I've searched enough but couldn't find any solution. Can anyone help me how can I add check box to the column id as select?
One thing : my 'serverSide': false.
$this->columnBuilder
->add("id", "column", array("title" => "Id","type" => "checkbox",))
->add("name", "column", array("title" => "Name",))
this is my code for generating the column.
ok, I found the solution, im not sure if this is the best way to solve the problem but still i can workout my issue.
->add(null, "multiselect", array(
"attributes" => array(
"name" => "check", ),
"actions" => array(
array(
"route" => "artist_show",
"route_parameters" => array(
"id" => "id"
),
"label" => "Select",
)
)))
adding the multi select option solve the necessity of have a check box

Elasticsearch Completion

I have a elasticsearch index which i update every 10 minutes via cronjob. In this index i have a completion field which works as expected.
But i have one little problem. Lets say i have a "article" field where i change a value from "a" to "b". After 10 minutes the index is been updated and the document which holds article "a" is been updated to article "b". Everything as expected.
But my completion field now holds both values. "a" and "b" both with the same id.
How can this happen?
Mapping:
'suggest' => array(
'type' => 'completion',
'payloads' => true,
'preserve_separators' => false,
'search_analyzer' => 'standard',
'index_analyzer' => 'standard'
),
How i set the field:
'suggest' => array(
'input' => array(
$result["Name"],
$result["Name"],
$result["Name2"],
$result["Name3"],
$result["Name4"],
$result["Name5"]
),
'output' => $result["Name"].' (' . $result["Name1"].', '.$result["Name2"].')',
'payload' => array(
'id' => $result["ID"]
)
)
Found the answer in the docs.
The suggest data structure might not reflect deletes on documents immediately. You may need to do an Optimize for that. You can call optimize with the only_expunge_deletes=true to only cater for deletes or alternatively call a Merge operation.

how to use Yii Eselect2 to save more data?

I use Eselect2 Yii extension to give users multiple choice but only last choice is submitted via POST. Why?
This is my html, in which I also tried to manage all choices with array but without success
<pre>echo $form->labelEx($model,'city_id');
$this->widget('ext.select2.ESelect2', array(
'name' => 'Form[field]',
'data' => City::model()->getCitie`enter code here`s(),
'options' => array('width' => '30%','allowClear'=>true),
'htmlOptions'=>array(
'options'=>array(''=>array('value'=>null,'selected'=>null, 'name'=>'field'),),
'multiple'=>'multiple',
)
));
</pre>
I tried to specify 'name' field as single field and as an array but I have the same problem: only last value is sended.
You should simply use an array :
Instead of
'name' => 'Form[field]',
You should try :
'name' => 'Form[field][]',

CakePHP select default value in SELECT input

Using CakePHP:
I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.
The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:
For example, calling example.com/leaf/add/5 would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id would default to "Tree 5", instead of "Tree 1" that it currently defaults to.
What do I need to put in my Leaf controller and Leaf view/add.ctp to do this?
In CakePHP 1.3, use 'default'=>value to select the default value in a select input:
$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));
You should never use select(), or text(), or radio() etc.; it's terrible practice. You should use input():
$form->input('tree_id', array('options' => $trees));
Then in the controller:
$this->data['Leaf']['tree_id'] = $id;
$this->Form->input('Leaf.id', array(
'type'=>'select',
'label'=>'Leaf',
'options'=>$leafs,
'value'=>2
));
This will select default second index position value from list of option in $leafs.
the third parameter should be like array('selected' =>value)
Assuming you are using form helper to generate the form:
select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)
Set the third parameter to set the selected option.
cakephp version >= 3.6
echo $this->Form->control('field_name', ['type' => 'select', 'options' => $departments, 'default' => 'your value']);
To make a text default in a select box use the $form->select() method. Here is how you do it.
$options = array('m'=>'Male','f'=>'Female','n'=>'neutral');
$form->select('Model.name',$options,'f');
The above code will select Female in the list box by default.
Keep baking...
FormHelper::select(string $fieldName, array $options,
array $attributes)
$attributes['value'] to set which value should be selected default
<?php echo $this->Form->select('status', $list, array(
'empty' => false,
'value' => 1)
); ?>
If you are using cakephp version 3.0 and above, then you can add default value in select input using empty attribute as given in below example.
echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);
The best answer to this could be
Don't use selct for this job use input instead
like this
echo $this->Form->input('field_name', array(
'type' => 'select',
'options' => $options_arr,
'label' => 'label here',
'value' => $id, // default value
'escape' => false, // prevent HTML being automatically escaped
'error' => false,
'class' => 'form-control' // custom class you want to enter
));
Hope it helps.
As in CakePHP 4.2 the correct syntax for a select form element with a default value is quite simple:
echo $this->Form->select(
'fieldname',
['value1',
'value2',
'value3'],
['empty' => '(auswählen)','default'=>1]
);
If hopefully don't need to explain the default=1 means the second value and default=0 means the first value. ;)
Be very careful with select values as it can get a little tricky. The example above is without specific values for the select fields, so its values get numerated automatically. If you set a specific value for each select list entry, and you want a default one, set its specific value:
$sizes = ['s' => 'Small',
'm' => 'Medium',
'l' => 'Large'];
echo $this->Form->select('size', $sizes, ['default' => 'm']);
This example is from the official 4.x Strawberry Cookbook.
https://book.cakephp.org/4/en/views/helpers/form.html#options-for-control

Categories