Cakephp 3 - select not submitted if no selection is made - cakephp

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();

Related

bootstrap selectpicker: Make a select drop down required

I am using bootstrap selectpicker and i want to make a dropdown required. What is the proper way to make a bootstrap selectpicker select required.
(i didn't want's to use any other jquery plugin).
{!! Form::select('from', [], null, ['class' => 'selectpicker form-control', 'data-live-search' => '1', 'data-size' => '6', 'title' => 'Select a partner',]) !!}
I am using that selectpicker into laravel form collective.
use html5's required
<select class="form-control selectpicker" name="txt_kode_supp" required>
</select>
for laravel collective:
insert array('required' => 'required') into your form collective 3rd parameter

Dropdown list size with CakePHP

I am currently using CakePHP and I have a dropdown list that contains a lot of text. This is actually a list of predefined messages that the user can select.
I wonder if there is a way to tell CakePHP to keep the original size of the dropdown list to not allow text spans to the right, force a carriage return somehow.
I also use Foundation, can be a solution at this level too?
Thanks
You can add class or Id to that select box and apply css on that to control width of that box like:
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('escape' => false));
Which will look like
<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
then add css like
#UserGender {width : 200px;}

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?

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

Resources