2014-07-11 25 views
11

請告訴我,如何僅通過Yii2中的ActiveField顯示標籤和字段錯誤? 我使用的是Redactor,我不僅要顯示textarea,還要顯示錯誤和標籤。謝謝。如何在Yii2中只顯示ActiveField的標籤和錯誤

代碼示例如下。

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

回答

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

謝謝。但是錯誤沒有顯示( – frops

+0

)您應該使用$ template-> template =「{label} \ n {error} \ n {input}」;否則您將無法獲得輸入字段 –

14

試試這個。我已經給出了合適的選項

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

它不會在客戶端進行驗證 –

+0

是的,我使用它 – Kshitiz

+0

我一直在尋找這個解決方案很長一段時間,我可以確認客戶端驗證正在工作 –

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