I have Tarifs, each tarif hasMany Price and Price also belongsTo UserGroup. So basically prices change when user's group is changed - doesn't matter that much.
The view looks like this
<?php echo $this->Form->create('Tarif');?>
...
$i=0;
foreach ($this->data['Price'] as $price) {
echo "<tr><td>".$this->Form->input("Price.$i.price", array('label' => false))."</td>";
echo "<td>".$this->Form->input("Price.$i.currency", array('label' => false))."</td>";
echo "<td>".$this->Form->input("Price.$i.UserGroup.id", array('label' => false))."</td>";
...
And I need the UserGroup.id input to display as a select, where each option displays group name and has its id as value. The user_group_id values are fine, but the are displayed in a text input. I've tried $this->Form->select and $this->Form->input(...,'type'=>'select') but both of them provided select boxes with no options.
How do I set the input to do what I want?
Thanks
In your controller, you need to add:
$user_groups = $this->UserGroup->find('list');
$this->set(compact('user_groups');
Then in the view, you setup the drop down like:
<?php echo $this->Form->input('user_group', array('options' => $user_groups)); ?>
You can then add $user_groups as an option to any Form->input and it will become a dropdown when using:
array('options' => $user_groups)
Related
I have a table in the database with the name: gstn_document_type_master.
I am using the following code to print the options in a drop down from a column "DOC_DESC" from the table (it is working fine).
<?php
echo $form->dropdownlist(
$model, 'dn_id',
CHtml::listData(GstndocumenttypemasterForm::model()->findAll(
array("order" => "DOC_TYPE_CODE asc")),
'ID', 'DOC_DESC'),
array('empty' => 'Select','class' => 'textInput')
);
?>
When the user selects a particular option and submits it, the particular "DOC_TYPE_CODE" should be printed.
<?php if(isset($_POST['Submit']))
{
// DOC_TYPE_CODE should be printed here
}
I have populated a dropdown box with data and I can select from a list.I cant seem to set the option on to the selected value.I select a value and I want the drop down box to have the last selected value display instead of resetting to the 1st option in the list. I have tried selected,default with and without numbers.
public function admin_list($tutorId=0,$paycycleId=0) {
if (isset($this->request->data['tutor'])) {
debug($this->request->data['TimeSheet']['tutor']);
$tutorId=$this->request->data['TimeSheet']['tutor'];
return $this->redirect(array('action' => 'admin_list',$tutorId,$paycycleId));
}//isset
view////
echo $this->Form->input('paycycle', array('label' => 'Pay Cycle Period',
'empty' =>array(0 => 'choose'), 'options' => $pcycle,'default'=>2 ));
echo $this->Form->submit('Select a Pay Cycle', array('name'=>'paycycle'));
echo $this->Form->end();
Per the detailed instructions in the CakePHP Book:
Set ‘selected’ to the value of the item you wish to be selected by default when the input is rendered
In a form, I have checkboxlist which has two check box (Male and Female)
User can select both of them or any of them. And values are getting save in DB.
When we populate it from DB then I want to do like if male was selected then it should select Male checkbox and disable the Female checkbox or vice versa.
The code in my View file is:
<?php if(isset($model['gender'])){
$data = $model['gender'];
if (isset($data)) {
if($data == 0)
$htmlOptions = array(
'0' => array('label'=>'MALE'),
'1' => array('disabled'=>'disabled','label'=>'FEMALE'),);
}
if($data == 1){
$htmlOptions = array(
'0' => array('disabled'=>'disabled','label'=>'MALE', ),
'1' => array('label'=>'FEMALE'),);
}
}
echo $form->checkBoxList($model, 'gender', $htmlOptions); ?>
Problem is when I am populating it is selecting the one,I selected while saving but not disabling the other one.
For this you have to use javascript because there is no way you can handle it only with PHP. Now I don't know that much JS, but the steps are easy:
Get the checkbox id's
check which one is selected
disable the other one with innerHTML in JS.
Here you have an example on how you disable checkboxes in JS
Just so you know, the JS should be placed inside the view.
I have two tables: product and product_type (related to the models resp. Product and Product_type):
product : product_id, product_type_id, name, etc...
product_type : product_type_id, name, desc, etc...
Both are related with key "product_type_id".
I have created the crud for both tables using gii crud generator. Now on Product form page I want to display the listing of all product_type name in dropdown field using Yii ActiveRecord. I have added this line in views/product/_form.php script:
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'id', 'id');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
But it's showing blank dropdown field :(
How can I do this?
Solved MySelf :)
By just providing product_type_name.
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'product_type_id', 'product_type_name');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
In Yii2 the class CHtml does not exist anymore.
Below is a solution, based on the following assumptions:
The Yii2 framework is used;
The table product_type is associated with the model Product_type;
The product_type table has a field called "type-name".
<?php
// get all product types from the corresponding table:
$productTypes = Product_type::find()->orderBy('type-name')->asArray()->all();
// create an array of pairs ('id', 'type-name'):
$productTypeList = ArrayHelper::map($productTypes, 'id', 'type-name');
// finally create the drop-down list:
$form->field($model, 'product_type_id')->dropDownList($productTypeList)
?>
Change your code like bellow
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'table_col_name1', 'table_col_name2'); //table_col_name1 is value of option, table_col_name2 is label of option
// echo $form->dropDownList($model, 'product_type_id', $list);
echo CHtml::dropDownList('name_of_the_dropdown', $model, $list);
?>
I have table types and i want to build selectbox with all values from this table
In my controller i wrote this code
$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
How build selectbox in view file ?
Well then its pretty simple all you need to do is first create List Data like
CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
for ex
typeList = CHtml::ListData(allRegistrationTypes,'id','type');
now remember both id and type are fields in table
now all you have to do is if you are using form then
<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
and if you need multiple you can pass multiple => multiple in the array as htmlOptions
You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.
If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.
Simplest Method to get "Select Box" in YII Framework:
<div class="row">
<?php
echo $form->labelEx($model,'county');
$data = CHtml::listData(County::model()->findAll(), 'id', 'county');
$htmlOptions = array('size' => '1', 'prompt'=>'-- select county --', );
echo $form->listBox($model,'county', $data, $htmlOptions);
echo $form->error($model,'county');
?>
</div>
Good Luck..