Drupal 7 select optgroup and allow multiple selections - drupal-7

I am using drupal 7 and need a form that provides a select box with optgroups and allows the user to select multiple items.
I can create the #options array along with optgroup by using
array(
['Option Group 1']=>array(
[1]=>'First Item',
[2]=>'Second Item',
),
['Option Group 2']=>array(
[3]=>'Third Item',
),
);
but if I then set
'#multiple'=>TRUE
I just get a select full of checkboxes with array() as the list item title
If I set
'#attributes'=>array('multiple'=>'multiple')
The form looks correct but drupal does not recognise any of my selections and sends me back to the form saying I need to select an option from the list.

Related

Drupal7: real time link selected field to a text field in Drupal7

I'm creating a webform to build up a subscription form, on Drupal 7.65
Goal
What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field.
As I said, the name should be displayed into a not modifiable text field just below it.
Suppose valid, the following list (key => value)
Field: Department
business_manager|Business Manager
hr_consultant|Human Resources
training_developer|Training Developer
and from the time going on, the associated names, are respectively
Options can appear into text field hr_business_partner
Steve Abc
Gertrude Def
Sven Hgj Klm
Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one.
I've attached a mokup to better understand what I do need.
IMPORTANT
I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations
You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.
Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
$form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
$form['hr_dept'] = array(
"#type" => "select",
"#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
);
$partners = taxonomy_get_tree(#); //the VID of the taxonomy
$list = array("0"=>"None"); //first option
foreach($partners as $tid => $partner){
$list[$partner->tid] = $partner->name;
}
$form['hr_partner'] = array(
'#type' => 'select',
'#options' => $list,
);
}
}
In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

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 2.4 Auto Load Checked values for HABTM

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',
);

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.

How to disable optgroup from combobox

HI,
I want to remove this optgroup from my cakephp combobox. How can i do this?
Regards,
karn
Well, just don't add it if you don't want it. Basically the select boxes are filled with single array like this:
$options = array(1=>'Text 1', 2=>'Text 2', ...);
Having optgroup require two dimensional array like:
$options = array(
'Opt Group 1'=>array(
1=>'Text1',
2=>'Text2',
...
),
'Opt group 2'=>array(
1=>'Text1',
2=>'Text2',
...
)
);
Most likely in your controller instead of:
$this->set('options', $this->YourModel->find('list'));
you are using
$this->set('options', $this->YourModel->find('all'));
Notice the parameter in the find function. Take a look in that article for Options parameter

Resources