Cakephp3 Saving Multiple dropdown selections to database - cakephp

I have a form with a dropdown that is multiple => true
echo $this - > Form - > input('test_id', [
'options' => $tests,
'required' => true,
'empty' => 'Select Tests',
'multiple' => true
]);
But when i submit the form only one value is saved in database.
I have searched for solution and found this:
Multiple select in input Cakephp
It suggests using SaveMany in my Controller, but i can't figure it out how to use it to get my desired output.
I'm very new to this framework so any help would be appreciated.

sorry i m also new but as other answers and question i don't see 'selected' => $selected in your form
CakePHP 2.0 Select form Mulitple selected
and How to set selection in mulitiselect list box in cakephp

Related

Expression Engine 3: Fieldtype settings - checkboxes

I'm working in Expression Engine 3 and I'm working on a custom field type, specifically the options for said field type. I've been following the docs along with how to build out the form for the settings, and everything is rendering correctly.
My question is with regard to checkboxes (and radio buttons), how do I determine which boxes should be pre-selected based on the currently saved settings?
The code I have for outputting the checkboxes is as follows:
$settings = array(
array(
'title' => 'Select your option(s):',
'fields' => array(
'multi' => array(
'type' => 'checkbox',
'choices' => array(
'1' => 'Option 1',
'2' => 'Option 2'
)
)
)
),
Now, in the display_settings($data) function, if I do a var_dump($data) I can see the values being correctly pulled in. But the checkboxes will not automatically set themselves to selected if they should be.
Any advice would be lovely, thank you! Also, I'd be happy to provide more specific details if needed.

How to use empty and selected in same dropdown box in cake php

I'm editing a dropdown box. i need to use empty and selected in same dropdown.
here is my code
$this->Form->input('per', array('id'=>'per','class'=>'inputs con_field','label'=>'per :', 'type'=>'select','options'=>$per_values,'selected'=>$labrcfps['Labourcfps']['per'],'empty'=>'- - Select --'));
If i use empty it automatically takes the empty as selected value.
I'm new in cakephp but i do this with array and parameter defaults :)
like that:
$options = array(
'x' => 'Select',
1 => 'Something',
2 => 'Car',
3 => 'Train'
);
echo $this->Form->input('name', array(
'options' => $options,
'default' => 'x'
));
Maybe that will be working fine :)
Variable $options is just your options after that you can validate if name of that field has value integer or 'x'. If 'x' then deny rest of logical code

Different Title for Different Radio buttons in Cakephp form helper

How can i give different title for different radio button using CakePHP Form Helper
$radio_options = array('unknown'=>'Unknown','negative'=>'Negative','positive'=>'Positive');
$titles = array('0'=>'Unknown','1'=>'Negative','2'=>'Positive');
I am trying to create radio buttons like this
echo $this->Form->input('radio_buttons', array(
'options' => $radio_options,
'legend' =>false,
'label' => true,
'div'=>false,
'class'=>'radio inline',
'type' => 'radio',
'separator'=>'<br>',
'title'=>$titles,));
But its not working..Form Helper creating same title for all of the radio buttons.
Look at what the FormHelper generates, then generate it manually or with a php foreach loop or something.
There are cases like this, when it's just easier (or the only way) to not use a helper.

Is it possible to create a checkbox with images along with the text in the labels in cakephp?

Somebody please give me ideas on this!I would like to generate multiple checkboxes with an image along with the text in the label. I have created a field called solutioncheckbox in the Contact form for Checkbox. My code is as below for creating multiple Checkboxes
<?php echo $this->Form>input('solutioncheckbox',array('label'=>false,'type'=>'select','class'=>'solution','multiple'=>'checkbox','options'=>array(1=>'WEB DESIGN',2=>'WEB DEVELOPMENT',3=>'GRAPHICS DESIGN'))) ;?>
Can I create an image along with the text in each of the labels like WEB DESIGN,WEB DEVELOPMENT etc in my form
This will get the job done for you ;)
echo $this->Form->input
(
'Model.field',
array
(
'multiple' => 'checkbox',
'options' => array('1' => $this->Html->image('cake.icon.png') . 'Text beside the image', '2' => $this->Html->image('test-error-icon.png') . 'Other text beside the image'),
'escape' => FALSE
)
);

display default time in cakephp ctp

i wish to display default time as 9:00 am in ctp time dropdown. Following is my ctp code:
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5
));
?>
how do i do that?
Use the 'selected' option
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5,
'selected' => '09:00:00',
));
?>
The best way to set default data is using the controller (and only if not posted):
if (!$this->request->is('post')) {
$this->request->data['Rideoffer']['DepartureTime'] = '09:00:00';
}
see http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options
This works in all form elements.
"selected", "value", "checked" and other hardcoded attributes for the form directly usually break the form after an unsuccessful post (if the form contains validation errors): it loses all the entered data and reset them to the value it was before which is usually quite annoying for the frontend user.
see http://www.dereuromark.de/2010/06/23/working-with-forms/

Resources