Yii dynamic checkbox - checkbox

I have a column in my table named 'availability_option' with type enum('0', '1', '2'). Zero means 'Fixed Price', One means 'Auction', Two means 'Both'.
I want to generate 2 checkboxes dynamically One for Fixed Price and another 'Auction'.
How is it possible?
I didi it static.
But it should not be the right syntax of yii.
<input value="0" id="fixedprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="fixedprice">Fixed Price</label>
<input value="1" id="auctionprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="auctionprice">Auction</label>
I want it dynamically, So how is it possible?Any idea?

Use checkboxList():
<?= $form->field($model, 'attribute_name')->inline(true)->checkboxList([0 => 'Fixed Price', 1 => 'Auction']) ?>
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html#inline%28%29-detail

I was facing The same problem and I solved It by doing like this :
<?php echo $form->field($model, 'name[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
Note that the checkboxList expects the first argument of the passed array to be of string type as well so you may need to make changes to your array.
Generated HTML will be as:
<label><input type="checkbox" name="fomrmName[name][]" value="'1'"> a</label>
another way by which I solved My problem was to wrap the checkbox in foreach block like following:
<?php foreach($vendorsData as $value){?>
<li><?= $form->field($ucVendors, 'vendor_id['.$value.']')->checkbox(array('label'=>$value)); ?>
and the Generated html is as:
<label><input type="checkbox" id="usedcarvendors-vendor_id-value" name="UsedCarVendors[vendor_id][value]" value="1"> value</label>
Reference Link here

Related

CakePHP 2 output checkbox array inside loop

I've read CakePHP multiple checkbox array HTML the right way but getting strange results. I have a list of tags (from a Model called Tag). I want to loop through these and output checkboxes for them in a View.
So I have obtained the Tag data in my Controller with:
$tags = $this->Tag->find('list', ['order' => ['name' => 'ASC']]);
$this->set('tags',$tags);
When I loop through it in my View I am trying to output the checkboxes in between Bootstrap markup:
<?php echo $this->Form->create('GroupTag'); ?>
<?php foreach ($tags as $tag_id => $tag): ?>
<div class="checkbox">
<label>
<?php echo $this->Form->checkbox('tag_id[]', array( 'value'=> $tag_id)); ?>
<?php echo $tag; ?>
</label>
</div>
<?php endforeach; ?>
I copied the syntax for tag_id[] from the post I linked to.
But when I inspect the markup it's producing the following as the name attribute for each <input type="checkbox">:
data[GroupTag][tag_id[]]
Should this not be
data[GroupTag][tag_id][]
?
The idea is that I have multiple checkboxes with a name attribute tag_id[] and then in the Controller I can loop through what has been checked.
Please can someone advise on this as I can't get it working and have looked into examples provided on here/docs.
Try this
$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
You can also do this:
$this->Form->input('GroupTag.tag_id', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $tag_id
]);
FormHelper::select(string $fieldName, array $options, array $attributes)
Example:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox'
));
Output:
<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" value="Value 1"
id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>
</div>
</div>

How to format the name of the checkbox while send by a GET cakephp

I would like to know if it's possible to format the name of the checkbox when I try to send it to my other page via a GET method.
Actually I've got a multiple checkboxes that generate something like:
<div class="checkbox"><input type="checkbox" name="test[]" value="1" id="ResearchTest1" /><label for="ResearchTest1">First Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="2" id="ResearchTest2" /><label for="ResearchTest2">Second Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="3" id="ResearchTest3" /><label for="ResearchTest3">Third Test</label></div>
And when I send my form, my URL looks like:
research%3D%26test%3D%26test%5B%5D%3D1%26test%5B%5D%3D2%26test%5B%5D%3D3
Which is :
research=&test=&test[]=1&test[]=2&test[]=3
And what I would like, will be:
research=&test1=1&test2=2&test3=3
Or
research=&test=1&test=2&test=3
Or, much better:
research=&test=1,2,3
Any ideas ?
That is how checkboxes work, if you want test1=1&test2=2&test3=3 then you should name each checkbox individually.
echo $this->Form->checkbox('foo', array('name' => 'test1'));
echo $this->Form->checkbox('foo', array('name' => 'test2'));
echo $this->Form->checkbox('foo', array('name' => 'test3'));
This will make your processing much harder.
You can do research=&test=1,2,3 with JS join(). Why you want to make this difficult for yourself I dont know. You can easily get that same format in the controller doint the GET with implode(',', $theData)
I would recommend using standards for submitting your form and process the data later.
You can define the name of the input using the FormHelper:
echo $this->Form->checkbox('yourInputId', array('name' => 'yourInputName'));
The problem is that I use a multiple checkboxes:
$this->Form->input('tests', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $options));
So I can't rename each checkbox individually.
Maybe I should not use cakephp for this, but try to write my own checkboxes directly in html?

cakePHP creating custom id's for checkboxes

I have a checkbox list that I create using the form helper
echo $form->input('Interest.interest_id', array('label' => __l('Interests'), 'multiple' => 'checkbox'));
It then created for each checkbox and automatic id
eg.
<input id="InterestInterestId1" type="checkbox" value="1" name="data[Interest][interest_id][]">
<input id="InterestInterestId2" type="checkbox" value="2" name="data[Interest][interest_id][]">
Is it possible to have my own unique id that I create for each checkbox? For example customInterestInterestId1, customInterestInterestId2 ...
You should be able to do this:
echo $form->input('Interest.interest_id',
array('label' => __l('Interests'),
'multiple' => 'checkbox',
'id'=>'your_custom_id_')); // add ID to the array
It works for the other auto-magic input types; but I haven't tested it with a multiple check box.
Cake will then generate:
... id="your_custom_id_1" ...
... id="your_custom_id_2" ...

HTML input text box vs CakePHP Automagic Form Elements

I was manually creating a simple form with one input text box field like this:
<form action="/user/add" method="post">
<input type="text" name="data[user_id]" value="1">
But when I call $this->model->save($this->data) in the Controller,
nothing was saved to the Table.
Only when I used this and the data in the field was written to the database successfully:
$form->create(null, array('url' => '/user/add'));
echo $form->input('user_id', array('label' => 'User ID', 'value' => '1'));
If you want to create the form manually,the name of the input part should be
<input type="sometype" name="data['modelname']['fieldname']" value="somevalue">
And in your code that should be
<form action="/user/add" method="post">
<input type="text" name="data['User'][user_id]" value="1">
See automagic form elements in the cookbook.

Form helper for creating Radio button in Cakephp

i am trying to create a Radio button using Cakephp like the one the result should resemble like
<div data-attr="radio" id="1">
<label id="label1">Untitled1</label><br/>
<input type="radio" value="option1" id="Radio11" name="Workexperience"/>
<label for="Radio11">Option1</label>
<input type="radio" value="option2" id="Radio12" name="Workexperience"/>
<label for="Radio12">Option2</label>
</div>
how to generate so using Form helper..
Please suggest me..
This might help,
http://book.cakephp.org/view/189/Automagic-Form-Elements#options-before-options-between-options-separator-a-191
For radio type input the 'separator' attribute can be used to inject markup to separate each input/label pair.
Code View
<?php echo $form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---',
'separator' => '--separator--',
'options' => array('1', '2'),
'type' => 'radio'
));?>
Output:
<div class="input">
--before--
<input name="data[User][field]" type="radio" value="1" id="UserField1" />
<label for="UserField1">1</label>
--separator--
<input name="data[User][field]" type="radio" value="2" id="UserField2" />
<label for="UserField2">2</label>
--between---
--after--
</div>
Looks like $form->radio() should do what you need. I don't know if it will look exactly like your example.

Resources