Adding a default option "Please Select" in cakephp dropdown - cakephp

I am new to cakephp. I need to add a default
<option value="0">--Please Select--</option>
in my following select field :
$attributes = array("empty"=>false,"Selected" => 'Select City',"id" => "location");
echo $form->select("gal_location_id", $gal_locations,null,$attributes);
I tried to add
$gal_locations[0] = "--Select City--";
$attributes = array("empty"=>false,"default" => 0,"id" => "location");
but the option coming at the bottom of the list. What is the proper way to add the default option ?

You're looking for the "empty" attribute:
$this->Form->input('gal_location_id', array(
'type' => 'select',
'options' => $gal_locations,
'empty' => 'Select City', // <-- Shows as the first item and has no value
'id' => 'location'
));

See the equivalent in CakePHP3 from this post
CakePHP3
With the following option you dont get a notification to choose a dropdown item.
So if you're just looking for a default value and force the user to pick another, just replace the assign a string to 'empty'.
echo $this->Form->input('db_field', [
'label' => 'My Drop Down',
'empty' => [$default => $default], //Your default options
'options' => $my_options //Your array /list'
]);

echo $this->Form->input('country_id',[
`enter code here`'options' =>$country,
'label' => false,
'class'=>'form-control select2',
'empty'=> 'Select...',
'value' => ''
]);

Related

sonata_type_model - Set value to empty

Is there a way to allow to set an empty value in the sonata_type_model?
This is my current configuration:
->add('location', 'sonata_type_model', [
'class' => Location::class,
'property' => 'name',
'label' => 'Herkunft',
])
Simply add 'empty_value' => '' to the array.
Same problem here. I solved it with the help of Saving Hooks: preUpdate()
public function preUpdate($object)
{
if (!$this->getRequest()->request->get($this->getForm()->getName())['location']) {
$object->setLocation(null);
}
}
// Changed sonata_type_model to symfony entity type field
// Then add placeholder option witch recognized as empty value option in select box.
...
->add('location', null, [
'class' => Location::class,
'property' => 'name',
'label' => 'Herkunft',
'placeholder' => 'Your empty option label',
])

CakePHP2 - Default value for input - select with option multiple

I have Form input with multiple select options. I am unable to set default values. This is my code:
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => array(
'cash'=>'cash',
'invoice'=>'invoice',
'ax'=>'ax',
'ca'=>'ca',
'vi'=>'vi',
'tp'=>'tp',
'dc'=>'dc'
),
'default'=>'ax'
)); ?>
How do I set default values for this input with PHP only?
This is working on my system. You can also set it from controller like this :
$this->request->data[$this->modelClass]['PaymentMethods'] = 'ax';
Please check these url also
CakePHP select default value in SELECT input
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
example :
$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
'size',
array('options' => $sizes, 'default' => 'm')
);
Since this is multi-choice select, value given must be array. And the key shouldn't be default, I should've used value instead.
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => $options,
'value'=> $array_of_data_fetched_from_database
)); ?>

prestashop multiple checkboxes do not save values

I can't figure out why the checkbox values are not saved in the database using helpers.
Trying to save some customers ids from my module's setting :
The array :
$custs = Customer::getCustomers();
foreach ($custs as $key => $value) {
$options[] = array(
'id_customer' => (int)$value['id_customer'],
'infos' => $value['firstname'].' '.$value['lastname'].' | '.$value['email']
);
}
The checkboxes :
'input' => array(
array(
'type' => 'checkbox',
'label' => $this->l('Customers'),
'desc' => $this->l('Select the Customers.'),
'name' => 'MY_MODULE_CUSTOMERS',
'values' => array(
'query' => $options,
'id' => 'id_customer',
'name' => 'infos',
),
),
)
The $_POST is always empty but works well with another input. Any help will be appreciated.
Thank you.
I don't think its in PS docs. But with a bit of code inspecting you can see in
Backoffice/themes/default/template/helpers/form/form.tpl
<input type="checkbox" name="{$id_checkbox}" id="{$id_checkbox}" class="{if isset($input.class)}{$input.class}{/if}"{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if} />
{$value[$input.values.name]}
add the porperty 'val' to option.
$options[] = array(
'id_carrier' => $carrier['id_carrier'],
'name' => $carrier['name'],
'val' => $carrier['id_carrier'],
);
Adn you get the desired serialization for the input values.
"transportistas" => array:2 [▼
0 => "73"
1 => "78"
]
Your code is correct, I tried it and this is result
http://screencast.com/t/wfsW86iJj
You have to click at least one checkbox.
Show data on server :
print_r($_POST);
die();
a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
If you want to be easy, using a single field to store in the database, take a look to THE COMPLETE CODE AND ALL THE STEPS AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
at the begining dont forget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['MY_MODULE_CUSTOMERS[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'checkbox',
'label' => $this->l('Select and employee'),
'name' => 'MY_MODULE_CUSTOMERS[]',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'values' => array(
'query' => $options,
'id' => 'id_customer',
'name' => 'infos',
),
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['MY_MODULE_CUSTOMERS'] = implode(',', Tools::getValue('MY_MODULE_CUSTOMERS'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

How to mark checkbox as selected using Bitwise in CakePHP?

I am developing an application in CakePHP 2.6 and I have a form where a user can set a series of flags when creating a calendar event.
I have managed to set up the 'add' action to display the flags and to also loop through in the controller after validation and save the value into my table. This process is done using bitwise. Code example below:
'add' action view:
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false));
'add' action controller:
$flags = 0;
foreach ($data['flag'] as $r) {
$flags |= (int)$r;
}
I am however having trouble getting the checkboxes for the flags to be marked as selected in the edit action view when they are displayed.
'edit' action view:
$selected = array($results[0]['BitwiseFlag']);
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false, 'selected' => $selected));
$results[0]['BitwiseFlag'] = 32 in the table.
$flagtypes array:
array(2) { [32]=> string(4) "Test" [64]=> string(9) "Testing 2" }
Not fully sure what your forms data structure looks like, but you should be able to set the flags from within your controller. Set the relevant values in $this->request->data, something like this:-
$this->request->data[$this->{$this->modelClass}->alias]['flag'] = [
0 => true,
1 => false,
2 => true
];
Then when you use $this->Form->input('flag', [...]) it should check the correct flags for you.
Change your input line to this :
$selected = array($results[0]['BitwiseFlag']);
// Change 'checked' to 'selected'.
echo $this->Form->input('flag', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'options' => $flagtypes, 'hiddenField' => false, 'selected' => $selected));

CakePHP creating radio buttons

How can I create two radio buttons with one being preselected based on the value of $foo? The snippet below creates them fine but does not select either of the two buttons.
$options = array('standard' => ' Standard','pro' => ' Pro');
$attributes = array(
'legend' => false,
'value' => false,
'checked'=> ($foo == "pro") ? FALSE : TRUE,
);
echo $this->Form->radio('type',$options, $attributes);
It's simple.. use the default value to $foo:
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
As you can see on the documentation:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::radio
you should preselect the value for any form field from the controller
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ "Default Values"
This is the way to go
$attributes = array();
$options = array('standard' => 'Standard', 'pro' => 'Pro');
if($foo === 'pro') {
$attributes['default'] = 'pro';
}
echo $this->Form->radio('type', $options, $attributes);
A better Solution is to set the defaults in the controller as Mark has pointed. That way you can set defaults at the end of your controller's action like...
Let's assume your Model is Member with membership_type field
$this->data['Member']['membership_type '] = 'pro';
For CakePHP 3.x, the following syntax should work.
$options = array('Y'=>'Yes','N'=>'No');
$attributes = array('div' => 'input', 'type' => 'radio', 'options' => $options, 'default' => 'Y');
echo $this->Form->input('add to business directory',$attributes);
HTH

Resources