how to add id and class to select box in Cakephp - cakephp

i am new to cakephp so i dont know how to i do this ... i have a view page in which there is a select box in which time zone is displaying
<?php
echo $this->Timezone->select('timezone');
?>
i want to add the 'id' , 'class' and label to it .. and if label isnt possible then its okk ..but the id and class is very important
what i want actually my select box is this
<label for="validation-select" class="label">Select</label>
<select id="validation-select" name="validation-select" class="select validate[required]">
or if i want to add the timezone in this i dont know how can i do this
helper class
http://bakery.cakephp.org/articles/MarkAlanEvans/2009/12/17/updated-timezone-helper

$month = array('1' => 'January', '2' => 'February', '3' => 'March', '4' => 'April', '5' => 'May', '6' => 'June', '7' => 'July', '8' => 'August', '9' => 'September', '10' => 'October', '11' => 'November', '12' => 'December');
echo $this->Form->select('month', $month, array('class' => 'form-control month', 'placeholder' => 'Month', 'id' => 'month', 'empty' => 'Select Month'));

Use the options array
A select generated by the form helper will already have an id of the format "ModelField" - if you want to override it just specify it in the options array (the second argument):
echo $this->Form->input('timezone', array(
'options' => $options
'id' => 'foo',
'class' => 'bar'
));
In the same way you can modify/add all tag attributes.
Since you're not directly using the form helper - simply modify the Timezone helper to permit passing another argument, and add pass through to the call to Form->input.

Related

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
)); ?>

Can't get formhelper to pre-populate multiple records

I have a model called Category which hasMany Expense. I am trying to generate an accordion type interface where a user can expand a category and edit the expenses. I can generate the input fields but they don't seem to be in "edit" mode because they are not pre-populated.
I've searched online and found a few related articles such as this one CakePHP: How to update multiple records at the same time with the Form helper and also this one CakePHP: Form helper with saveMany() to edit multiple rows at once. I have tried to emulate their code but with no success.
My CategoriesController Index function looks like this ...
public function index($project_id) {
$data = $this->Category->find('all', array(
'conditions'=>array('Category.project_id' => $project_id),
'order'=>array('Category.category_name')
));
$this->set('categories', $data);
$this->request->data = $data;
}
I have read that cakephp2 requires $this->request->data to be set for FormHelper to work. But in the examples I have found online everyone seems to use the plural of the model name so I tried that as well.
My Categories\index.ctp looks like this. I'm not at the "accordion" stage yet. I'm just trying to get input boxes on the screen that are pre-populated.
$i=0;
foreach ($categories as $category)
{
echo $this->Form->input("Category.$i.category_id");
echo $this->Form->input("Category.$i.category_name");
$j=0;
foreach($category['Expense'] as $expense)
{
echo $this->Form->input('Expense.' . $j . '.expense_id');
echo $this->Form->input('Expense.' . $j . '.expense_name');
echo $this->Form->input('Expense.' . $j . '.dollar_amount');
echo $this->Form->input('Expense.' . $j . '.sqft_amount');
$j++;
}
$i++;
}
This code seems to be iterating properly because it spits out the correct input fields. The big problem right now is just getting the fields to pre-populate. It doesn't seem to be in "edit" mode and I'm worried that that will be a problem down the road when I try to save the data.
Also, I have tried it with and without $this->form->create('Category') at the top. It doesn't seem to make a difference.
The $categories array looks like this ...
array(
(int) 0 => array(
'Category' => array(
'category_id' => '1',
'category_name' => 'Category 1',
'category_index' => '1',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '2',
'expense_name' => 'Nails',
'category_id' => '1',
'dollar_amount' => '50',
'sqft_amount' => '1',
'expense_index' => '2'
),
(int) 1 => array(
'expense_id' => '1',
'expense_name' => 'Wood',
'category_id' => '1',
'dollar_amount' => '99',
'sqft_amount' => '1',
'expense_index' => '1'
)
)
),
(int) 1 => array(
'Category' => array(
'category_id' => '3',
'category_name' => 'Category 2',
'category_index' => '2',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '3',
'expense_name' => 'Bed',
'category_id' => '3',
'dollar_amount' => '99',
'sqft_amount' => '2',
'expense_index' => '1'
),
(int) 1 => array(
'expense_id' => '4',
'expense_name' => 'Chair',
'category_id' => '3',
'dollar_amount' => '70',
'sqft_amount' => '1',
'expense_index' => '2'
)
)
)
)
Any help would be greatly appreciated. Thanks!!
Coincidentally, I've been doing almost exactly the same thing today.
In order to automatically populate the form, you need to have your data in $this->data in the view. You can do this by assigning to $this->request->data in your controller.
Then you need to have your field names reflect the structure of that array exactly. So using your example, you might have:
echo $this->Form->input("$i.Category.category_name");
for the category fields, and
echo $this->Form->input("$i.Expense.$j.expense_id");
for your expense fields.

CakePHP trouble using FormHelper for a year menu

Cake 2.2.4
I have a database sport_year field set to the YEAR data type.
I've tried two approaches to creating a year menu for my add/edit views, but both have their own problems.
Using $this->Form->input:
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'name' => 'data[Sport][sport_year]',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -'
));
The code works fine to add or edit data (as does the validation), but when arriving at the edit page existing year data is not properly selected in the form. The data array does show [sport_year] => 2012, but 2012 is not selected in the menu.
echo $this->Form->year(
'sport_year',
date('Y') - 2,
date('Y') + 1,
array(
'name' => 'data[Sport][sport_year]',
'label' => 'Year',
'empty' => '- select -'
)
);
The year helper seems to correctly retrieve and select the existing data, but the label does not work, and it doesn't correctly render the field as required even though my Model has validation set. It does still require the data, but it ignores my Model custom message and is falling back to a save error message in my controller.
$this->Form->year() will never show a label. Only $this->Form->input() adds labels as it is a wrapper method and year() is for creating a year field.
The code in cake is something like:
public function input() {
echo '<div>';
echo $this->label(...);
echo $this->year(...);
echo '</div>';
}
Taking this code, you do not need name as cake will be generating that already. If you are not in the Sport model you can use input('Sport.sport_year', ...)
If you read the docs you should see there is a default option for inputs. you can use that or make sure that $this->request->data['Sport']['sport_year'] is set to the year you want.
You could also set the value option.
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -',
'default' => date('Y')
));
or
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -',
'value' => date('Y')
));
For option 1 do this:
echo $this->Form->input('sport_year', array(
...
'selected' => 'data[Sport][sport_year]'
));

Cakephp edit.ctp not populating input boxes despite existing in $this->data

I'm struggling to find the answer to this. I have only been using CakePHP for a month and I've hit a problem. It's something I can fix by manually inserting the values but I expected my data to pre-populate. Here is what is happening:
The Model is Product which hasMany 'Dynamicprice'
I'm testing a product with the id of 7 (/products/edit/7).
the first part of my edit function is:
public function edit($id = null) {
$this->Product->id = $id;
if (!$this->Product->exists()) {
throw new NotFoundException('Invalid Product');
}
if ($this->request->is('get')){
$this->request->data = $this->Product->read();
}
debug($this->request->data);
//other stuff setting vars for drop-down lists
}
the
debug($this->request->data);
gives me the following:
array(
'Product' => array(
'id' => '7',
'category_id' => '70',
'name' => 'Full Test',
'description' => 'This is to test all features',
'price' => '0.00',
'aesthetic' => true,
'image' => '',
'price_structure' => '2',
'suggest_for' => '',
'created' => '2012-06-28 12:49:06',
'modified' => '2012-06-28 12:49:06'
),
'Dynamicprice' => array(
(int) 0 => array(
'id' => '15',
'product_id' => '7',
'drop' => '600',
'prices' => '6000:9.99, 12000:18.99 '
),
(int) 1 => array(
'id' => '16',
'product_id' => '7',
'drop' => '1200',
'prices' => '6000:19.99, 12000:28.99 '
),
(int) 2 => array(
'id' => '17',
'product_id' => '7',
'drop' => '2400',
'prices' => '6000:29.99, 12000:38.99 '
)
)
)
However, whilst everything in ['Product'] pre-populates the ['Dynamicprice'] array does not pre-populate the following:
<?php
echo $this->Form->input('Dynamicprices.0.id');
echo $this->Form->input('Dynamicprices.0.drop', array('label' => 'Drop 1 (mm). Enter "0" for "Any Drop"'));
echo $this->Form->input('Dynamicprices.0.prices', array('label' => 'Prices 1', 'type' => 'textarea', 'rel' => 'dynamic'));
?><hr><?php
echo $this->Form->input('Dynamicprices.1.id');
echo $this->Form->input('Dynamicprices.1.drop', array('label' => 'Drop 2 (mm).'));
echo $this->Form->input('Dynamicprices.1.prices', array('label' => 'Prices 2', 'type' => 'textarea', 'rel' => 'dynamic'));
?><hr><?php
echo $this->Form->input('Dynamicprices.1.id');
echo $this->Form->input('Dynamicprices.2.drop', array('label' => 'Drop 3 (mm).'));
echo $this->Form->input('Dynamicprices.2.prices', array('label' => 'Prices 3', 'type' => 'textarea', 'rel' => 'dynamic'));
?>
Am I right to expect them to populate automatically and if so what have I done wrong?
I have created /Model/Dynamicprice.php with the following just to make sure:
class Dynamicprice extends AppModel {
public $name = 'Dynamicprice';
public $belongsTo = 'Product';
But as I expected it didn't change anything.
This is the second time I've done this this week; ask a stupid question. Yes I was right to expect them to pre-populate. The problem is I got my naming conventions mixed up. "Dynamicprices.1.drop" shouldn't have the 's' at the end. Silly me!

Creating 'select' listboxes using FormHelper in CakePHP

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

Resources