2

I have a following code below:

<?= $form->field($model, 'date') ->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
 'id' => 'form',
 'autocomplete' => 'off',
 'value' => date('m/d/Y'),
 'autoclose'=>true,
]]) ?>

That will result in HTML in following way:

<div class="form-group has-icon has-label">
<label for="formSearchUpLocation">Picking Up Location</label>
<input type="text" class="form-control" id="formSearchUpLocation" placeholder="Airport or Anywhere">
<span class="form-control-icon"><i class="fa fa-map-marker"></i></span>
</div>

My question is as following: How can I integrate the css and bootstrap classes in HTML below into yii2 active form above? Thank you for your help.

3 Answers 3

2

if you are looking to add the class to all the group divs mean all the div that have the class form-group then you can use the fieldConfig option of the ActiveForm or if you want it for one specific field then you can use the options option of the $form->field() as the 3rd parameter

For the whole Form

$form = yii\widgets\ActiveForm::begin([
            'fieldConfig' => [
                'options' => [
                    'class' => 'my-group'
                ]
            ]
        ]);

For Single Field

echo $form->field($model, 'name', ['options' => ['class' => 'my-class']])->textInput();

Conversion

About converting your above HTML using ActiveForm the following should work you can use template option of the $form->field() 3rd parameter to add your custom icon after the input, along with other, see below will create your desired HTML

echo $form->field($model, 'date', [
    'options' => [
        'class' => 'form-group has-icon has-label'
    ],
    'inputOptions' => [
        'class' => 'form-control'
    ],
    'template' => '{label}{input}<span class="form-control-icon"><i class="fa fa-map-marker"></i></span>{error}'
])->widget(yii\jui\DatePicker::class, [
    'id' => 'created_at',
    'options' => [
        'placeholder' => 'Airport or Anywhere'
    ]
]);

You will have something like below

![enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0
<?= $form->field($model, 'date') ->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
 'id' => 'form',
'class' => 'WRITE-YOUR-CLASS'
 'autocomplete' => 'off',
 'value' => date('m/d/Y'),
 'autoclose'=>true,
]]) ?>

just input your class name in 'class' section it will work. example : 'class' => 'fa fa-map-marker'

2 Comments

Manu, thanks for your answer, I did it, but the icon of calender is not being in the same row with the calendar input, it is going just under the input
i think it need some seperate styling, else you need to use normal html5 date picker
0

You could try something like the code bellow to concat your calendar icon with the widget:

<?php
 $addon = '<span class="input-group-addon">
   <i class="fa fa-calendar-alt"></i>
 </span>';

 echo $addon.$form->field($model, 'date')->widget(yii\jui\DatePicker::className(),['clientOptions' => [ 'placeholder' => 'dd/mm/yyyy',
  'id' => 'form',
  'autocomplete' => 'off',
  'value' => date('m/d/Y'),
  'autoclose'=>true,
 ]]);
?>

Or you can try search more on the plugin you are using, some widgets have his own attribute to render the calendar icon.

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.