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]) ?>