RAHUL VENNEERS
RAHUL VENNEERS

Reputation: 33

yii2 adding a extra value in form field

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.

Upvotes: 0

Views: 733

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5721

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;

Upvotes: 1

Related Questions