how to set default selection in radio button - Active form yii

时间:2017-12-18 05:53:07

标签: php yii radio-button

Here i need to add default selection in radio button(checked).

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>    
<?= $form->field($model, 'email') ?>
<?php if(!empty($usernames)){ 
    echo '<label>Select Any Username To Which You Want To Login</label><div class="all_username">';
    foreach ($usernames as $key => $value) { ?>
        <?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
    <?php } ?>
    </div>
    <?= $form->field($model, 'password')->passwordInput() ?>
<?php } ?>

2 个答案:

答案 0 :(得分:1)

You need to use following method where 1 is your selected ID.

$form->radioButtonList($model,'1', $usernames, array('separator'=>"" )); 

答案 1 :(得分:1)

You can probably use something like this:

foreach ($usernames as $key => $value) { 
    if(/*somecondition*/){
        $model->username = $value['username'];
    }?>
    <?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>

Only by assigning the $model->username a default value you can choose which radio will be selected.