How to display only label and error for ActiveField in Yii2
34,201
Solution 1
<?php
$field = $form->field($model, 'username', ['options' => ['class' => 'form-group col-sm-6']]);
$field->template = "{label}\n{error}";
echo $field->textInput(['maxlength' => 255]);
?>
Solution 2
Try this. I have given the option for saperate
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = \yii\widgets\ActiveForm::begin([
'id' => 'form-id',
'options' => ['class' => 'form-horizontal'],
'enableClientValidation'=> true,
'enableAjaxValidation'=> false,
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => true,
'action' => 'youractionurl',
'validationUrl' => 'yourvalidationurl'
]);
echo $form->field($model, 'fieldname')->begin();
echo Html::activeLabel($model,'fieldname'); //label
echo Html::activeTextInput($model, 'fieldname'); //Field
echo Html::error($model,'fieldname', ['class' => 'help-block']); //error
echo $form->field($model, 'fieldname')->end();
\yii\widgets\ActiveForm::end();
?>
Solution 3
It too some decision, but mistakes still aren't displayed.
$redactor = yii\imperavi\Widget::widget(
[
'model' => $model,
'attribute' => 'text',
'options' => [
'minHeight' => 400,
],
]
);
$form->field($model, 'text', ['template' => "{error}\n{label}\n{hint}\n{$redactor}"])->textarea();
Comments
-
frops over 3 years
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(); ?>
-
Dmitrii Malyshev over 9 yearsYou should use $template->template = "{label}\n{error}\n{input}"; otherwise you won't get input field.
-
RN Kushwaha over 8 yearsIt does not validate on client side.
-
andrzej1_1 about 8 yearsI was looking for this solution for a long time. I can confirm that client-side validation is working.
-
andrzej1_1 almost 8 years@RNKushwaha if you are using bootstrap active form, error need additional class to work:
Html::error($model, 'foobar', ['class' => 'help-block help-block-error'])
-
Maxxer over 4 yearsI did this:
Html::tag("div", Html::error($model, "myfield", ['class' => 'help-block']), ['class' => 'has-error'])