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
)); ?>
Related
I've created a large form in Cake and set default options via inputDefaults. However I wish to change the default values for an individual field.
In setting the form defaults, I wrote approximately this:
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-7 offset-3')));
...with the result that all like fields produce the same error message. But, when I attempt to change the defaults for a single field, like so:
echo $this->Form->input('name', array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-10'))));
It doesn't work. The field name produces an error whose class reads column-7 and offset-3, whereas I'd intended column-10.
Anybody know a solution?
$options['inputDefaults'] You can declare a set of default options for input() with the inputDefaults key to customize your default input creation:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
All inputs created from that point forward would inherit the options declared in inputDefaults. You can override the defaultOptions by declaring the option in the input() call:
echo $this->Form->input('password'); // No div, no label
// has a label element
echo $this->Form->input(
'username',
array('label' => 'Username')
);
I'm looking for a way, to handle the Pagination-options in a dropdown menu.
I'd like my users to select the sorting order in the same form with my filtering options, so when they hit "Send", pagination-order is already set.orm
e.g. like:
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'sort:id/direction:desc' => 'New Items, desc',
'sort:id/direction:asc' => 'New Items,asc',
),
'div' => false
)
);
$(function() {
$('#sort-properties').change(function() { // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
var price = $(this).val();
window.location = baseUrl + 'properties/property_view/'+price;
});
});
<?php
echo $this->Form->input('orderby', array(
'id' => 'sort-properties',
'options' => array(
'sort:sale_rent_price/direction:asc' => 'Sort by Price Low to High',
'sort:sale_rent_price/direction:desc' => 'Sort by Price High to Low',
'sort:created/direction:asc' => 'Sort by Date Old to New',
'sort:created/direction:desc' => 'Sort by Date New to Old'
),
'label' => false,
'empty' => 'Default Order'
));
?>
I would change the values of the options to e.g. fieldname.desc, like :
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'id.desc' => 'New Items, desc',
'id.asc' => 'New Items,asc',
),
'div' => false
)
);
Afterwards in the controller put the values to an array via the explode method:
$order = explode(".", $this->request->data['Model']['field']);
then you can use the values in your find condition, like:
$result = $this->Model->find('all', array(
'order' => array( $order[0] => $order[1] )
));
There is propably a more elegant way to make this, but this should work.
For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?
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
I have two models, Category and Point. The associations are defined as:
Category hasMany Point
Point belongsTo Category
I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.
Where would I need to set the category list and how could I do it? And how would I produce the select box?
I assume it could be done with
$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
//setting somewhere.
Also to generalize a bit:
In a View with access to the Form helper
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
));
?>
The above will render a select input with two options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the HTML.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
));
?>
One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
'selected' => 'California',
));
?>
From the Model
Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name, that is, ( $this->set( 'categories', $categories );, then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".
Aziz's answer at #2 is the example of that automagic kicking in.
CakePHP 1.3 Form Helper
CakePHP1.2 Form Helper
In the controller:
$categories = $this->Point->Category->find('list');
$this->set(compact('categories'));
In the view:
$form->input('category_id',array('type'=>'select'));