how to delete the empty value option in $form->select - cakephp

when i need to use select form i see the first value is empty..but i dont need this empty value option ..how to do this..thanks
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options)
?>
Will output:
<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>

In Cake 2.x, you can just add the 'empty'=>false like this (tested and works):
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('empty'=>false));
?>
In CakePHP 1.3.x (per this page in the book) you might have to add an additional null like this:
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, null, array('empty'=>false));
?>

Related

multi select not work in cakephp.how can add all select?

i create two multi select list in my cakephp view .
in one of select box get all users and in anouther get all car list .
i want select users and car name and insert in user profile.but just select one and last item .
my table=>
user -->
-id
-username
-user_family
car-->
-id
-name
-model
my controller{
$users = $this->User->find('all', array(
'fields' => array('User.id', 'User.username', 'User.user_family')));
$this->set('users', $users);
$cars = $this->Car->find('all', array());
$this->set('cars', $cars);
}
and my view file {
foreach($users as $user){
?>
<option
value="<?php echo $user['User']['id'] ?>"><?php echo $user['User']['username'].' (' .$user['User']['user_family']. ' )' ?></option>
<?php
}
?></select>
<label>نام درس</label> </label>
<select name="car_id" id="car_id" multiple="true" style="width: 150px;height: 200px;">
<?php
foreach($cars as $car) {
?>
<option value="<?php echo $car['Car']['id'] ?>"><?php echo $course['Car']['name'] ?></option>
<?php
}
?>
</select></fieldset>
}
and my pr() result:
Array
(
[user_id] => 16
[car_id] => 8
)
but i select 10 user and 2 car .how can fix it?
Try in controller
$users = $this->User->find('list');
$cars = $this->Car->find('list');
$this->set(compact('users','cars'));
in your view use Form helper
echo $this->Form->input('User');
echo $this->Form->input('Car');

Cakephp option tag attributes

In the view:
echo $this->Form->input('Ingredient');
The above populate multiple select list that HTML output as:
<select name="data[Ingredient][Ingredient][]" option="hh" multiple="multiple" id="IngredientIngredient">
<option value="1" selected="selected">Tomato</option>
<option value="2">Spaghetti </option>
<option value="3" selected="selected">Salt</option>
</select>
What I need to know is how to add attributes to the generated <option> tag?
Use the controller to pass selected values:
if ($this->request->is('post') {
// save form
} else {
$this->request->data['Incredient']['Incredient'] = $ids;
}
See here
To add additional attributes like classes you just need to make it a deeper array and you can pass those:
$options = array(
1 => 'One',
2 => array('name' => 'Two', 'value' => 2, 'class' => 'extra'),
3 => 'Three');
echo $this->Form->input('test', array('type' => 'select', 'options' => $options));
The result:
<div class="input select">
<label for="ModelTest">Test</label>
<select name="data[Model][test]" id="ModelTest">
<option value="1">One</option>
<option value="2" class="extra">Two</option>
<option value="3">Three</option>
</select>
</div>
See this
My Example custom tag option in select, by (CakePHP 2 + VueJs 2) is easy.
<?php
$options = array(
array(
'value' => false,
'v-for' => 'obj in data_options_input_id_vuejs',
'name' => '{{obj.text}}',
'v-bind:value' => 'obj.value',
)
);
echo $this->Form->input('input_id', array(
'type' => 'select',
'required' => true,
'class' => 'input-block-level',
'options' => $options,
'v-model:' => 'productos_stocks_padre_combo_select',
));
?>
Hi all, In my case, i want to add code="code-value" to my select option and this code is working for me.
If you want to add extra data to your option, try this code:
//In controller
$chapters = $this->Course->Chapter->find('all', array('fields' => array('name', 'code', 'id')));
// Call the noop function $this->noop() on every element of chapters
$chapters = Hash::map($chapters, "{n}.Chapter", array($this, 'noop'));
// Callback noop function
function noop($option) {
$option['value'] = $option['id'];
unset($option['id']);
return $option;
}
You can see document about Hash:map function here. Good luck.

CakeDC Search Plugin With Dropdown

I'd like to add a select box for searching with the CakeDC Search Plugin. IE:
<select name="field">
<option value="email">Search By Email</option>
<option value="first_name">Search By First Name</option>
</select>
Currently what I have in my VIEW is:
echo $this->Form->create('User', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('email', array('div' => false, 'empty' => true));
echo $this->Form->input('first_name', array('div' => false, 'empty' => true));
This works just fine this way, but I'd like to avoid the multiple input boxes and simplify it with a select box. I could hard it (take the value from the select box and combine it with the value from the input box), but there has to be another way to do it...
Here is my User Module:
public $filterArgs = array(
'email' => array('type' => 'like'),
'first_name' => array('type' => 'like')
);
And this is my Controller:
public function index() {
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->User->parseCriteria($this->passedArgs);
$this->set('users', $this->paginate());
}
i think you are looking for
echo $this->Form->input('search', array('div' => false, 'empty' => true));
and
public $filterArgs = array(
'search' => array('type' => 'like', 'field'=>array('email', 'first_name')),
);
and
public $presetVars = true;
but you would lose the "AND" of the two inputs in favor of an OR (this is less powerful).
if thats ok for you, this would be the way.

cakephp: I want a simple select box with options and no optgroup

The select box for forum_categories in the cupcake forum plugin doesn't allow me to select its options. When I rollover the options with my mouse, the highlight stays at 'Select a Forum'. The following is the original
<?php
echo $form->input('forum_category_id', array(
'options' => $forums,
'empty' => '-- '. __d('forum', 'Select a Forum', true) .' --',
'label' => __d('forum', 'Forum Category', true)
));
?>
And i modified it to:
<?php
echo $form->input('Topic.forum_category_id',array(
'empty' => 'Select a Forum',
'options' => $forums
));
?>
The following is the html code it is generating:
<select name="data[Topic][forum_category_id]" id="TopicForumCategoryId">
<option value="">Select a Forum</option>
<optgroup label="Summer Camp">
</optgroup>
</select>
The find stmt in the forumcategory model:
$forums = $this->Forum->find('list', array(
'conditions' => array(
'Forum.status' => 0,
'Forum.accessView <=' => $access,
'Forum.access_level_id' => $accessLevels
),
'order' => 'Forum.orderNo ASC'
));
How can I get rid of the optgroup in the html code above? I just want a simple select box with options and no optgroup like the following:
<select name="data[Topic][forum_category_id]" id="TopicForumCategoryId">
<option value="">Select a Forum</option>
<option value="1">Summer Camp</option>
</select>
thank you.
probably a bit late but
you get opt groups when
your options looks like this :
$arr = array(
'optgroup' => array(
'1','2','3'),
'optgroup2' => array(
'1',2,3)
);
echo $this->Form->input('some',array('options' => $arr));
Try:
echo $this->Form->input('Topic.forum_category_id', array('options' => $forums, 'empty' => 'Select a Forum'));

CakePHP - populating select form

I'm trying to populate a drop down select form with values from a database.
Here is what I have currently.
$modes = Set::combine($this->Setting->find('all', array('conditions' => array('setting_name LIKE' => 'mode_%'))), '{n}.Setting.id','{n}.Setting.setting_name');
$this->set('modes', $modes);
Then in the view, this is what I have
echo $form->select('current_mode',$modes);
That output
<select name="data[Setting][current_mode]" id="SettingCategoryId">
<option value=""></option>
<option value="2">mode_2</option>
<option value="1">mode_1</option>
<option value="3">mode_3</option>
</select>
The output that I have right now almost work but how can I make the output to be like this?
<select name="data[Setting][current_mode]" id="SettingCategoryId">
<option value="mode_2">Title 2</option>
<option value="mode_1">Title 1</option>
<option value="mode_3">Title 3</option>
</select>
Note:
1. no default option with empty value
2. Option's value isn't the id and titles comes from a "title" field in the table
Thanks,
Tee
See http://book.cakephp.org/view/1022/find-list and http://book.cakephp.org/view/1062/displayField.
$settings = $this->Setting->find('list', array(
'conditions' => array('Setting.setting_name LIKE' => 'mode_%'),
'fields' => array('Setting.id', 'Setting.title')
));
$this->set(compact('settings'));
// view
echo $this->Form->input('current_mode', array(
'type' => 'select',
'options' => $settings,
'empty' => false
));
You want to loop through modes and create an option for each mode, like so:
$options = array();
foreach($modes as $mode)
{
$options[$mode] = "Title " . $mode;
}
echo $form->select('current_mode', $options);
You can either put the above logic in your view, or you can do it in your controller, and then set the variable like this:
$this->set("options", $options);
The docs here explain the select element method pretty well:
http://book.cakephp.org/view/1430/select

Resources