php - Yii2: Config global template for all forms fields -


i have this:

<?php  use app\models\location; use yii\helpers\html; use yii\widgets\activeform; use yii\helpers\arrayhelper; use app\models\role; ?>          <?php $form = activeform::begin(); ?>          <div class="row">             <div class="col-sm-6">                 <?= $form->field($model, 'roleid', yii::$app->formtemplate->fieldtemplate())->dropdownlist(arrayhelper::map(role::find()->all(), 'id', 'name'), array('prompt' => '-- select role --', 'class' => 'form-control select2')); ?>             </div>             <div class="col-sm-6">                 <?= $form->field($model, 'published')->checkbox(['label' => ''], true)->label($model->getattributelabel('published'), ['class' => 'form-label semibold']); ?>             </div>         </div> 

i think unefficient since have add template each field (i know can add per form, checkbox has different one, ideas how set globally, forms? not 1 form?

so far have done create component called formtemplate avoid writing template directly in view, good, want set globally.

<?php  namespace app\components;  use yii; use yii\base\component;  class formtemplate extends component {       public function fieldtemplate($option = []) {          $template = [             'template' => '',             'labeloptions' => [ 'class' => 'form-label semibold']         ];          $icon = '';         $position = 'right';          if(!empty($option['position'])) {             $position = $option['position'];         }          if(!empty($option['icon'])) {             $icon = $this->_setfieldicon($option['icon']);         }          $template['template'] = '<div class="form-group">{label}<div class="form-control-wrapper form-control-icon-'.$position.'">{input}'.$icon.'<div class="error">{error}{hint}</div></div></div>';          return $template;      }      private function _setfieldicon($option) {          switch($option) {             case 'text':                 $icon = '<i class="fa fa-text-width"></i>';                 break;             case 'password':                 $icon = '<i class="fa fa-key" aria-hidden="true"></i>';                 break;             default:                 $icon = '';                 break;         }          return $icon;      }   } 

any ideas?

update

i have noticed activefield component, maybe on global config? has done that?

$config = [     'id' => 'basic',     'basepath' => dirname(__dir__),     'bootstrap' => ['log'],     'components' => [         'activefield' => [             'template' => '...'         ] 

you can create activefield class :-

<?php namespace frontend\widgets;  use yii\helpers\arrayhelper; use yii\widgets\activefield;  class myactivefield extends activefield {  public $labeloptions = [ 'class' => 'form-label semibold'];  public function init() {     $position = arrayhelper::remove($this->options, 'right');      $icon = $this->_setfieldicon($this->options);      $this->template ='                 <div class="form-group">{label}                 <div class="form-control-wrapper form-control-icon-'.                 $position.'">                 {input}'.$icon.                 '<div class="error">{error}{hint}                 </div>                 </div>                 </div>';      parent::init(); }  /**  * @param $option array  * @return string html  */ private function _setfieldicon($option) {     $icon ='';     switch(arrayhelper::getvalue($option ,'icon' ,'')) {         case 'text':             $icon = '<i class="fa fa-text-width"></i>';             break;         case 'password':             $icon = '<i class="fa fa-key" aria-hidden="true"></i>';             break;     }      return $icon; } } 

and in activeform use class : -

<?php $form = activeform::begin([     //change active field class     'fieldclass' => 'frontend\widgets\myactivefield' ]); ?>  <div class="row">     <div class="col-sm-6">         <?= $form->field($model, 'roleid',[             'options' => ['icon' => '' ,'position' => '']]         )->dropdownlist(arrayhelper::map(role::find()->all(), 'id', 'name'), [             'prompt' => '-- select role --', 'class' => 'form-control select2'         ]); ?>     </div>      <div class="col-sm-6">         <?= $form->field($model, 'published' ,['icon' => '' ,'position' => ''])->checkbox(['label' => ''], true)             ->label($model->getattributelabel('published'), ['class' => 'form-label semibold']); ?>     </div> </div>  <?php activeform::end(); ?> 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -