Cakephp 2.4 Auto Load Checked values for HABTM - cakephp

I cannot get the values to auto check. The list of options displays fine.
$this->data['Business']['ExpertiseType'] has values.
Business habtm ExpertiseType defined in all three models still set to their default bake.
Business belongsTo FormEoiEntry
$this->data = $this->FormEoiEntry->find('first', ['conditions'=>['FormEoiEntry.id'=>1324], 'recursive'=>2]);
$this->Form->create('FormEoiEntry');
$this->Form->input('Business.ExpertiseType', ['multiple'=>'checkbox']);
$this->Form->end();
Am I missing something here? I cannot figure out why it is not being detected and checking the boxes.

set expertise_types from your controller like this.
$this->set('expertise_types', $this->FormEoiEntry->find('first', [
'conditions'=>['FormEoiEntry.id'=>1324],
'recursive'=>2,
]);
and in your view
$this->Form->input('Business.ExpertiseType', [
'options' => $expertise_types,
'multiple'=>'checkbox'
]);
update
In that case pass the selected options values to the $this->request->data['Business']['ExpertiseType']
just like this
$this->request->data['Business']['ExpertiseType'] = array(
'select option value 1', // you can put your option values
'select option value 2',
'select option value 3',
'select option value 4',
);

Related

CakePHP 3 - Form Select Option Group - Multiple Checkboxes

First, I've successfully implemented a Multiple Select box with option groups based on providing an providing a group of options structured like this:
$options = [
'Group 1' => [
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
],
'Group 2' => [
'Value 3' => 'Label 3'
]
];
echo $this->Form->select('field', $options);
This is straight from the Cookbook at: http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-select-pickers
It works great, and makes a select box exactly as it should. My issue is that I would like to change the display to be multiple checkboxes. In order to do that, I've switched the code to:
echo $this->Form->input('field', [
'multiple' => 'checkbox',
'options' => $options
]);
When I do this, the display ends up being a single checkbox, with all the options listed out next to it.
In searching stackoverflow, I found the following: How to create multiple checkboxes grouped by fieldsets in Cakephp 3
Most seem to indicate that the functionality is not included in Cake, and that you need to build it on your own. There is one comment on the initial question that references the cookbook and that it specifically states:
If you would like to generate a select with optgroups, just pass data in hierarchical format. This works on multiple checkboxes and radio buttons too, but instead of optgroups wraps elements in fieldsets:
No one seems to address the comment on that question. My question is really simple. Does CakePHP 3 allow for multiple checkboxes created as outlined in the documentation, or is the documentation incorrect and this functionality isn't included in the core? If the answer is that the functionality is included in the core, what's the trick to getting it to work?
Thanks!
in that multi select checkbox
<?= $this->Form->select('input_name',$checkboxarray, array('selected' =>$send_checkbox_select,'multiple' => 'checkbox')); ?>
$checkboxarray=[
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
`],

CakePHP how to submit NULL value?

On web form I have select-option field. It have to submit value 'card' (without quotes), or NULL value.
Now, code looks like this:
$options = array(NULL => 'Invoice', 'card' => 'Payment card');
echo $this->Form->input('payment_method', array('options'=>$options));
How can I define options array, to submit NULL value in database table (and/or 'card', but that works of course), in case that Invoice option is selected?
Thank you in advance!
NULL is an invalid key for an associative array. Only 'integers' and 'strings' are valid array keys, see the documentation: Arrays
To send an empty value through the form, you can use an empty string. To force inserting a NULL value in the database, handle this inside your model or controller before saving
$options = array('' => 'Invoice', 'card' => 'Payment card');
echo $this->Form->input('payment_method', array('options'=>$options));
Then inside your controller;
if ('' === $this->request->data['MyModelName']['payment_method']) {
this->request->data['MyModelName']['payment_method'] = null;
}
Alternative approach
However, why not use a value for the 'invoice' payment method? Like this:
echo $this->Form->input('payment_method', array(
'options' => array(
'' => 'Select your payment method..',
'invoice' => 'Invoice',
'card' => 'Payment card',
)
));
IMO this has some advantages;
it's more transparent, e.g. When looking inside the database, it's clear that the payment method is 'invoice'. By using a 'special' value (null), people without knowledge of your applications inner workings will have to browse through the source code to find out
it's possible to check if the use has selected a payment method; e.g. If the value is empty, the use may have forgotten to select a payment method, in which case you can mark the field 'invalid' via a validation rule.

How to diaplay other tables attributes in single view form and also insert values in corresponding tables

I have two tables QBQuestion(Questionid,Question,OptionId) and Option(OptionId,Option). I want to display option form on view form of QBQuestion? I want to create multiple choice question. i.e.for single question we can add multiple options.For such purpose i want to cretae option field with add button si that when we click add button,we can insert more options and also want to display that all inserted options in table using grid.
So what should i do? please help me....
1) Add relations in the model for this two stuff.
public function relations() {
return array(
'valOptions' => array(self::BELONGS_TO, 'Option', 'OptionId'),
);
}
2) Use lazy loading in CGridView.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => new CActiveDataProvider('QBQuestion'),
'columns' => array(
'Questionid',
'Question',
'valOptions.Option',
),
));
I think that's what you need.

Having Trouble Generating A Simple Select Dropdrown In CakePHP 2.0

I'm pretty sure I don't have this problem in Cake 1.3, but:
I have a form input based on an is_live db field (containing 1 or 0 as its value).
The following creates a correctly populated checkbox:
echo $this->Form->input('is_live', array('label'=>'Status'));
However, the following does not seem to create a correctly populated dropdown (the first option is always selected, even though selecting an item and submitted the form does update correctly):
echo $this->Form->input('is_live', array(
'label'=>'Status', 'type'=>'select' , 'options'=>array(1=>'Live', 0=>'Pending')
));
Is there anything simple I can do to make the dropdown populate based on the value of is_live in CakePHP 2.0? Or is there a workaround?
I had the same issue with using 1 and 0 before.
My solution is to use the following
$options = array(1=> 'Live', 0=>'Pending');
echo $this->Form->input('YourModel.is_live',
array(
'options' => $options,
'label' => 'Status',
'selected' => intval($defaultValue), // make sure you set a default value
)
);
Can you change the content length of that field? If you can, change it to 2. This will get around the problem.
ALTER TABLE `your_table` CHANGE `is_live` `is_live` TINYINT(2) NULL DEFAULT NULL;

Change individual labels with automagic form helper on a 'multiple' => 'checkbox'

I have the following line in my add/edit Course views:
echo $this->Form->input(
'Competency',
array(
'label' => 'Which competencies does this course address?',
'multiple' => 'checkbox'
)
);
(Modelled as Course hasMany Competencies)
I'd like to customise the label that gets output for each competency but can't seem to find a way of doing so - the 'label' field seems to work as a group heading rather than changing the label for the individual checkboxes.
(What I'm ideally after is rather than just displaying Competency.name I can display Competency.name plus the Competency.code as the label)
NB I thought about changing the displayField but that would change it everywhere and it's only here I'd like to be different.
Create a virtual field [details]:
//in your Competency model
var $virtualFields = array(
'name_code' => 'CONCAT(Competency.name, " ", Competency.code)'
);
Then in your controller, before retrieving the data, set your displayField to your just-created virtual field:
//in your controller prior to the find
$this->Competency->displayField = 'name_code';
Since you're setting displayField in the controller, it doesn't set it permanently, so no need to set it back, but if you're doing more finds immediately after this, you can always set back to name if you want.

Resources