Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only label and error for ActiveField in Yii2

Tags:

php

yii

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(); ?>
like image 849
frops Avatar asked Jul 11 '14 13:07

frops


2 Answers

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(); 
?>
like image 129
Kshitiz Avatar answered Sep 19 '22 09:09

Kshitiz


<?php 
$field = $form->field($model, 'username', ['options' => ['class' => 'form-group col-sm-6']]);
$field->template = "{label}\n{error}";  
echo $field->textInput(['maxlength' => 255]);
?>
like image 33
zelenin Avatar answered Sep 21 '22 09:09

zelenin