请告诉我,如何在Yii2中仅显示ActiveField的字段标签和错误? 我使用Redactor,我不仅要显示textarea,还要显示错误和标签。感谢。
代码示例如下。
<?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(); ?>
答案 0 :(得分:15)
试试这个。我已经选择了单独的
<?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();
?>
答案 1 :(得分:6)
<?php
$field = $form->field($model, 'username', ['options' => ['class' => 'form-group col-sm-6']]);
$field->template = "{label}\n{error}";
echo $field->textInput(['maxlength' => 255]);
?>
答案 2 :(得分:2)
这也是一些决定,但错误仍然没有显示出来。
$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();