Dropdown list value from view to controller in yii - yii

I'm new to yii.I have one dropdownlist and I populate value using the database and save the selected value to database. I can populate values from db but I can't get the values from dropdown and save to database.
$model = new TCategoryDescription();
$model->parent_id =='0';
echo $form->labelEx($model,'MainCategoryName');
echo $form->dropDownList($model, 'fk_i_cat_id', CHtml::listData(TCategoryDescription::model()->findAll(array('condition'=>'parent_id=0')), 'fk_i_cat_id', 'cat_name'),
array('prompt'=>'Select Main Category'),array('selected'=>'true','ajax' => array('type'=>'POST','url'=>yii::app()->baseUrl.'/tcategorydescription/subtest',

Related

Yii2: set text field's value when an item from a dropdownlist is selected

I have the following code on my form:
<?php
$unitPrices = ArrayHelper::map(Item::find()->where('isApproved and
vendor_id=:id', [':id' => $vendorId])->all(),'id','unitPrice');
?>
this gives me a map called unitPrices where the key-value pairing is id-unitPrice
<?= $form->field($model, 'item_id')->dropDownList(
ArrayHelper::map(Item::find()->where('isApproved and vendor_id=:id', [':id' => $vendorId])->all(),'id','itemCode','description'),
[
'prompt'=>'--Select Item--',
'id'=>'item_selected',
'onchange' => '$("#priceOnLine").val($unitPrices[item_id_value]);',
])
?>
what I want to do is when an item is selected from the dropdownlist, it also sets the value of a text field called priceOnLine using the unitPrices map where the id is the value of the selected item from the dropdownlist.
I tried setting the text field to a static value (100), and it works but I can't figure out how to set it using the map.
'onchange' => '$("#priceOnLine").val(100);',
Thanks in advance for the help :D
You need to get the text of the selected option you can add the following script on the top of your view and remove the 'onchange' => '$("#priceOnLine").val($unitPrices[item_id_value]);', from your dropDown()
<?php
$js=<<<JS
$("#item_selected").on("change",function(){
$("#priceOnLine").val(($(this).find("option:selected").text()));
});
JS;
$this->registerJs($js,\yii\web\View::POS_READY);
Hope it helps

How to generate Dynamic radio buttons in Yii advanced template?

This is my controller:
$Shipping = Shipping::find()->all();
foreach ($Shipping as $key => $value) {
pr($value['name']);
}die;
O:P/
Free
Pickup
Country Wise
API
I want to generate radio button list in view with those O:P .How?
If the radio buttons are in active form
you can use active radioList
<?= $form->field($model, 'your_field')->
radioList(ArrayHelper::map( Shipping::find()->all(), 'your_id', 'your_name')); ?>

Adding new field in the database in Yii 1.1

I have added a field allergy in the user table. But after submitting the form it is not get inserted. I am new to Yii. I am not using any Gii generators since this is the existing code base. Is it possible to add the new field as I tried here or should I run any Yii generators (model,CRUD)
I changed the following areas.
\protected\views\userManagement\user.php
<?php echo $form->labelEx($model,'allergy'); ?>
<?php echo $form->error($model,'allergy'); ?>
\protected\models\User.php
In the attributeLabels() function:
'allergy' => 'Allergy',
In the search() function:
$criteria->compare('allergy',$this->allergy);
In the beforeSave() function:
if($this->allergy=='')
$this->setAttribute('allergy', NULL);
else
$this->setAttribute('allergy', $this->allergy);

Is it possible to have the same Model and CGrid parameter in a form

I have a form
$form=$this->beginWidget('CActiveForm', array('id'=>'subject-form','enableAjaxValidation'=>false,));
which has a parameter "Subject"
$form->labelEx($model,'subject');
$form->textField($model,'subject',array('size'=>200,'maxlength'=>255));
I would also like to add a "Subject Search Grid" further down the view (so I can search other Subjects as I'm editing the current). To that end I am passing $model into the view as normal, plus an alias $relatives to the grid.
$subject_search_grid= $this->widget(
'zii.widgets.grid.CGridView',
array(
'id'=>'subject-grid',
'dataProvider'=>$relatives->search(),
'filter'=>$relatives,
'columns'=>array(
'id',
'subject',
array('class'=>'CButtonColumn',),
),
),
$captureOutput=true
);
CGrid is working perfectly, however it's instance of 'subject' is overwriting the $form 'subject' when I try save or update the form. Is there any way of moving CGrid out of the view logic so it does not overwrite the value?
thanks
Place the gridview outside the form? So after <?php $this->endWidget(); ?>.

Multiplying values from a textbox in Yii Framework

i'm new to Yii and I'm trying to compute values from textboxes. I want the computation codes to be on the same form where the textbox is being displayed (since the product will also be shown on a textbox of the same form where they will input the factors). I've tried this guess, but it didn't work. How do I fix this problem?
<div class="row">
<?php echo $form->labelEx($model,'Quantity_In_Pieces'); ?>
<?php echo $form->textField($model,'Quantity_In_Pieces',array('Quantity_In_Pieces' => ('Quantity').val() * ('Hold').val());?>
<?php echo $form->error($model,'Quantity_In_Pieces'); ?>
</div>
As i understood, you need to add the textField on your ActiveForm and to set its value calculated from two another textfields. In this case you should use Chtml::textfield(...) with value = ($model->Quantity) * ($model->Hold). I mean smth like
echo Chtml::textField('Quantity_In_Pieces', ($model->Quantity) * ($model->Hold));