frops frops - 4 months ago 52
PHP Question

How to display only label and error for ActiveField in Yii2

Please, tell me, how to display only label and error for field by ActiveField in Yii2?
I'm using Redactor and I want to display not only textarea, but also errors and label. Thanks.

The code example is given below.

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

<?php echo $form->errorSummary($model); ?>

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

<?php
echo yii\imperavi\Widget::widget(
[
'model' => $model,
'attribute' => 'text',
'options' => [],
]
);
?>
<br />
<div class="form-group">
<?= Html::submitButton(
$model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
) ?>
</div>

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

Answer
<?php 
$field = $form->field($model, 'username', ['options' => ['class' => 'form-group col-sm-6']]);
$field->template = "{label}\n{error}";  
echo $field->textInput(['maxlength' => 255]);
?>