php 如何在 Yii2 中仅显示 ActiveField 的标签和错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24698403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:26:20  来源:igfitidea点击:

How to display only label and error for ActiveField in Yii2

phpyiiyii2

提问by frops

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.

请告诉我,如何在 Yii2 中通过 ActiveField 仅显示字段的标签和错误?我正在使用 Redactor,我不仅要显示 textarea,还要显示错误和标签。谢谢。

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(); ?>

采纳答案by zelenin

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

回答by Kshitiz

Try this. I have given the option for saperate

尝试这个。我已经给了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(); 
?>

回答by frops

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();