yii 表单布局左右2列显示

yii布局表单1列显示则输入框显得过长,所以调整为2列显示,但是textarea还是1列显示

自定义widgets并继承ActiveForm

namespace common\widgets;

use Yii;
use yii\helpers\Html;
use yii\base\InvalidConfigException;

class HorizontalActiveForm extends \yii\widgets\ActiveForm
{
    /**
     * @var string the default field class name when calling [[field()]] to create a new field.
     * @see fieldConfig
     */
    
    /**
     * @var array HTML attributes for the form tag. Default is `[]`.
     */
    public $options = [];

    /**
     * @var string the form layout. Either 'default', 'horizontal' or 'inline'.
     * By choosing a layout, an appropriate default field configuration is applied. This will
     * render the form fields with slightly different markup for each layout. You can
     * override these defaults through [[fieldConfig]].
     * @see \yii\bootstrap\ActiveField for details on Bootstrap 3 field configuration
     */
    public $layout = 'horizontal';


    /**
     * {@inheritdoc}
     */
    public function init()
    {
        if (!in_array($this->layout, ['default', 'horizontal', 'inline'])) {
            throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
        }

        if ($this->layout !== 'default') {
            Html::addCssClass($this->options, 'row form-' . $this->layout);
        }
 
        $this->fieldConfig = [
            'template' => "<div class='row row-box'><div class='col-xs-2 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-9'>{input}</div><div class='help-block'>{error}</div></div>",
        ];
        parent::init();
    }

    /**
     * {@inheritdoc}
     * @return ActiveField the created ActiveField object
     */
    public function field($model, $attribute, $options = [])
    {
        $hidden = '';
        if(isset($options['options']['hidden'])) {
            $hidden = ' hide';
        }
        if(isset($options['options']['whole_row'])) {
            $options = [
                'options' => ['class' => 'col-md-12 col-sm-12 whole-row form-group' . $hidden],
                'template' => "<div class='row row-box'><div class='col-xs-1 col-sm-1 text-right'>{label}</div><div class='col-xs-10 col-sm-10'>{input}</div><div class='help-block'>{error}</div></div>",
            ];
        } else {
            $options = [
                'options' => ['class' => 'col-md-6 col-sm-12 double-row form-group' . $hidden]
            ];
        }
        return parent::field($model, $attribute, $options);
    }
}

使用

use common\widgets\HorizontalActiveForm;

<?php $form = HorizontalActiveForm::begin(); ?>

    <?= $form->field($model, 'banner_id')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'subject_id')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'area_id')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'type')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'start_at')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'end_at')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'create_user_id')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'update_user_id')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'index')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'status')->textInput(['maxlength' => true]) ?>

  
    <?= RightSubmitButton::widget() ?>

    <?php HorizontalActiveForm::end(); ?>

如果像textarea这样的需要1列显示的

<?= $form->field($model, 'describe', ['options' =>  ['whole_row' => true]])->textarea(['rows' => 6]) ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值