我有这个:
<?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 a 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>
我认为这是无效的,因为我必须将模板添加到每个字段(我知道我可以为每个表单添加它,但复选框有一个不同的,任何想法如何全局设置,对于所有表单?不仅仅是一种形式?
到目前为止,我所做的是创建一个名为FormTemplate的组件,以避免在视图中直接编写模板,这很好,但我想全局设置它。
<?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;
}
}
有什么想法吗?
更新
我注意到ActiveField是一个组件,所以也许我可以在全局配置上做到这一点?有人这样做了吗?
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'activeField' => [
'template' => '...'
]
答案 0 :(得分:2)
如果要为所有应用程序自定义ActiveField
个对象,则应使用Yii2 dependency injection container,例如:
\Yii::$container->set('yii\bootstrap\ActiveField', [
'template' => '...',
]);
或ActiveForm
:
\Yii::$container->set('yii\bootstrap\ActiveForm', [
'inputTemplate' => '...',
]);
答案 1 :(得分:2)
你可以创造
您的ActiveField
班级为: -
<?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;
}
}
在ActiveForm
中使用您的课程: -
<?php $form = ActiveForm::begin([
//change this with your 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 a 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(); ?>