setting focus on pageload

I searched around a bunch and finally found (and promptly lost the reference to) the answer to the question of how to set focus on an input field at page load time.

I started down this path while migrating from 1.1 to 2.0.2 and noticed the login form no longer set focus on the username field so I was trying to "fix" it. Anyway, after a lot of searching around, I found "inputOptions" work just fine.

default code in advanced template:

<?= $form->field($model, 'username' ?>

With a little addition…

<?= $form->field($model, 'username', ['inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control', 'tabindex' => '1']]) ?>

Thanks :)

Thanks Jim K.

Your tip was useful.

But know I have a problema. I want to put de focus on a field that as a mask. It doesn’t work.

My code:

<div class="myform-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'fieldone')->dropDownList($postosdetrabalho, ['prompt'=>'- Não Definido -']); ?>

    <?= $form->field($model, 'fieldtwo' , ['inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control']])->textInput()->widget(\yii\widgets\MaskedInput::className(), [

    'mask' => '99']) ?>

    <?= $form->field($model, 'fieldthree')->textInput(['maxlength' => true]) ?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>


    <?php ActiveForm::end(); ?>

Unfortunately it does not work.

If I put the inputOptions into another field it works perfectly!

Can you help me please?