0

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. Commented 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? Commented Aug 29, 2016 at 9:38

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.