cakePHP creating custom id's for checkboxes - cakephp

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" ...

Related

Cakephp 3 - select not submitted if no selection is made

I have a form ( generated using the form helper ) with this select input
<div class="input select">
<label for="pilot-ratings">Pilot Ratings</label>
<select name="pilot_ratings" class="listbox" size="5" id="pilot-ratings">
<option value="1">Habilitación de Vuelo Nocturno Local</option>
<option value="3">Habilitación Cat. II / Cat. III</option>
<option value="5">Habilitación de Remolque de Planeador</option>
</select>
</div>
Only when an option is selected, that option is added to $this->request->data['pilot_ratings'].
Is there any way to force submitting all of the options of the select input every time no matter an option is selected or not ?
Thanks.
Regards.
How are you moving options between the select boxes?
to automatically submit an option you need to add selected="selected" attribute to the <option> element. To submit many options in a select box in the form you need to add multiple to the <select> element
if you want to automatically submit all options in the list on page load it would need to look like this
<select name="pilot_ratings" class="listbox" size="5" id="pilot-ratings" multiple>
<option value="1" selected="selected">Habilitación de Vuelo Nocturno Local</option>
<option value="3" selected="selected">Habilitación Cat. II / Cat. III</option>
<option value="5" selected="selected">Habilitación de Remolque de Planeador</option>
</select>
this can be acheived using the cakephp Form helper like:
<?= $this->Form->input('pilot_ratings',[
'type' => 'select',
'class' => 'listbox',
'size' => 5,
'id' => 'pilot_ratings',
'multiple' => 'multiple',
'options' => [
['name' => 'Habilitación de Vuelo Nocturno Local', 'value' => '1', 'selected' => 'selected'],
['name' => 'Habilitación Cat. II / Cat. III', 'value' => '2', 'selected' => 'selected'],
['name' => 'Habilitación de Remolque de Planeador', 'value' => '5', 'selected' => 'selected']
]
]); ?>
Havent tested the syntax of the form helper code but it is the right idea.
One thingI just thought about is how you read the data after the form has been submitted. The data will still be submitted like:
pilot_ratings=1
pilot_ratings=3
pilot_ratings=5
By default I think (havnt actually tested this) $this->request->data['pilot_ratings']; will only hold one of these values, either the 1st one-or the last one as it keeps overwritting itself.
If this that is the case you may need to change the form method to get
and then extract all the values from the query string which you can get from:
$this->request->here();

Yii dynamic 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

How to add attribute to an input in Cakephp

I'm developing an app in cakePHP and I'd like to use Foundation Tooltips into it.
I need to add data-tooltip to the input in order to make it work, but I don't know how to do it with the cakePHP form helper.
Right now I'm using:
<?php echo $this->Form->input('title', (array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
)));
And is returning this:
<input name="data[Initiative][title]" class="has-tip" title="Tooltip for initiative title" maxlength="255" type="text" id="InitiativeTitle" required="required">
When I need this:
<input data-tooltip name="data[Initiative][title]" class="has-tip" title=...>
I've tried adding 'data-tooltip'to the input array with no luck, but I'm sure it has to be an easy way of doing it.
Thanks in advance and sorry for my English.
<?php echo $this->Form->input('title', array( 'label' => __('title'),
'class' => 'has-tip',
'title' => __('Tooltip for initiative title'),
'data-tooltip'=>""
));

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?

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.

Categories

Resources