2016-01-13 65 views
9

下面我有一些代碼:如何將類添加到YII2中的ActiveField的form-group div?

<?= 
    $form->field($model, 'phone_no')->textInput(
    [ 
     'placeholder' => 
     '(Conditionally validated based on checkbox above, groovy!)' 
    ] 
) 
?> 

導致HTML:

<div class="form-group field-contactform-phone_no"> 
    <label class="control-label">Phone No 
    <input type="text" aria-describedby="hint-contactform-phone_no" placeholder="(Conditionally validated based on checkbox above, groovy!)" name="ContactForm[phone_no]" id="contactform-phone_no" class=""></label> 
    <small class="error-box"></small> 
    <p class="help-text" id="hint-contactform-phone_no"></p> 
</div> 

我的問題是:

我怎樣才能一類 '看不見的' 添加到外層div(含class = form-group目前)?

感謝您的幫助

回答

15

你可以不喜歡這樣的單場:

<?= $form->field($model, 'phone_no', ['options' => ['class' => 'form-group invisible']) 
    ->textInput(['placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?> 

在全球範圍內(在表格所有字段)有可能是這樣的:

<?php $form = ActiveForm::begin([ 
    'fieldConfig' => ['options' => ['class' => 'form-group invisible']], 
]); ?> 

你可以還有條件地構建fieldConfig

<?php $form = ActiveForm::begin([ 
    'fieldConfig' => function ($model, $attribute) { 
     if (...) { 
      return ['options' => ['class' => 'form-group invisible']], 
     } 
    }, 
]); ?> 

請注意,您還必須包括form-group類,因爲它不會與您的自定義類合併。

官方文檔:

+0

感謝那些作品不錯定義一個類!現在如何讓它淡入jQuery(即刪除「隱藏」元素,但使元素淡入)。將尋找它,感謝您的幫助arogachev – gvanto

6

所有投入要素定義的模板佈局。

<?php 
       $form = ActiveForm::begin([ 
          'id' => 'purchase-sms-temp-form', 
          'layout' => 'horizontal', 
          'fieldConfig' => [ 
           'template' => " <div class=\"form-group form-md-line-input\">{label}\n{beginWrapper}\n{input}<div class=\"form-control-focus\"> </div>\n{error}\n</div>{endWrapper}", 
           'horizontalCssClasses' => [ 
            'label' => 'col-md-2 control-label', 
            'offset' => 'col-sm-offset-4', 
            'wrapper' => 'col-sm-10', 
            'error' => 'has-error', 
            'hint' => 'help-block', 
           ], 
          ], 
       ]); 
       ?> 

       <div class="form-body"> 
        <?= $form->field($model, 'mobile') ?> 
        <?= $form->field($model, 'volume') ?> 
        <?= $form->field($model, 'hospital_id') ?> 
        <?= $form->field($model, 'created_date') ?> 
        <?= $form->field($model, 'complete') ?> 
        <?= $form->field($model, 'modified_date') ?> 

       </div> 

自定義字段,你可以通過

$form->field($model, 'phone_no', [ 
      'options' => [ 
      'class' => 'form-group invisible' 
      ])->textInput([ 
       'placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?> 
+0

爲什麼定義模板只是添加類到字段容器?第二種方法已經在我的答案中涵蓋了。 – arogachev

+0

缺少自定義字段中的一個右括號,['options'=> ['class'=>'form-group invisible']] –