yii2 ActiveForm字段占位符

12

我想使用yii2 ActiveForm创建表单,这是我的代码:

<?php
$form = \yii\widgets\ActiveForm::begin([
    'options' => [
        'class' => 'form-inline'
        ]
]);
?>
<div class="form-group">
     <label class="sr-only" for="example">Email</label>
     <?php echo $form->field($model, 'email', [
           'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
     ])->textInput(['placeholder' => "Enter Your Email"])->input('email')->label(false); ?>
</div>


<button type="submit" class="subscr-btn btn btn-primary btn-fill">Join</button>
<?php \yii\widgets\ActiveForm::end(); ?>

生成此HTML的程序代码:

<form id="w0" class="form-inline" action="/example" method="post">
<div class="form-group">
    <label class="sr-only" for="exampleInputEmail2">Email address</label>
    <div class="form-group field-subscriber-email required">
        <input type="email" id="subscriber-email" class="form-control transparent" name="Subscriber[email]"
               autofocus="autofocus">
        <div class="help-block"></div>
    </div>
</div>
<button type="submit" class="subscr-btn btn btn-primary btn-fill">Join</button>

一切都很好,但是placeholder在哪里?

1个回答

30

将其作为第二个参数放入input()方法中 - 参考文档

<div class="form-group">
     <label class="sr-only" for="example">Email</label>
     <?php echo $form->field($model, 'email', [
           'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
     ])->textInput()->input('email', ['placeholder' => "Enter Your Email"])->label(false); ?>
</div>

14
它也可以放在textInput中: ...)->textInput(['placeholder' => "输入您的电子邮件"]) - Soli
是的,正确。您可以编辑答案并添加备注(最好带有参考文献)。这是相关的。 - Sohel Ahmed Mesaniya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接