I am working in a yii2 project. I have a form with text field. One field in form is length. currently I use text field for that. What I need is a text field for value and followed by a dropdown box in that we can choose cm or inch or pixel like that. How to do that and How to get value in controller.
2
-
<?= $form->field($model, 'height')->textInput() ?>. I need a dropdown box attached to this field. So that i can enter value in text field and select dimension from dropdown list.RAHUL VENNEERS– RAHUL VENNEERS2016-08-29 08:02:51 +00:00Commented Aug 29, 2016 at 8:02
-
You may have to use an additional column in database if you want to save it. Is that ok?Gynteniuxas– Gynteniuxas2016-08-29 09:38:32 +00:00Commented Aug 29, 2016 at 9:38
Add a comment
|
1 Answer
You can do it by 2 ways:
First,
Take an input field and one dropDownList using html helper class or you can create simple dropDownList using html
<?= $form->field($model, 'length')->textInput(['maxlength' => 255]); ?>
<?= Html::dropDownList('length_type', null,[ 'cm' => 'cm', 'inch' => 'inch', 'pixel' => 'pixel'],['class' => 'form-control','id'=>'length_type']) ?>
if you want to use html helper class than import Html class as below
use yii\helpers\Html;
Now, use length type in controller
if(isset($_POST['length_type']) && $_POST['length_type'] !=null)
{
$len_type=$_POST['length_type'];
// use this variable according to yuor need
}
Second,
decalare varibale length_type in model class
in view,
<?= $form->field($model, 'length')->textInput(['maxlength' => 255]); ?>
<?= $form->field($model, 'length_type')->dropDownList([ 'cm' => 'cm', 'inch' => 'inch', 'pixel' => 'pixel'], ['class' => 'priority_list']) ?>
In controller you can use model variable directry as
$len=$model->length;
$len=$model->length_type;