You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.2 KiB
110 lines
3.2 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\bootstrap;
|
||
|
|
||
11 years ago
|
use Yii;
|
||
11 years ago
|
use yii\base\InvalidConfigException;
|
||
|
|
||
|
/**
|
||
11 years ago
|
* A Bootstrap 3 enhanced version of [[\yii\widgets\ActiveForm]].
|
||
11 years ago
|
*
|
||
|
* This class mainly adds the [[layout]] property to choose a Bootstrap 3 form layout.
|
||
|
* So for example to render a horizontal form you would:
|
||
|
*
|
||
|
* ```php
|
||
|
* use yii\bootstrap\ActiveForm;
|
||
|
*
|
||
|
* $form = ActiveForm::begin(['layout' => 'horizontal'])
|
||
11 years ago
|
* ```
|
||
11 years ago
|
*
|
||
9 years ago
|
* This will set default values for the [[ActiveField]]
|
||
|
* to render horizontal form fields. In particular the [[ActiveField::template|template]]
|
||
11 years ago
|
* is set to `{label} {beginWrapper} {input} {error} {endWrapper} {hint}` and the
|
||
9 years ago
|
* [[ActiveField::horizontalCssClasses|horizontalCssClasses]] are set to:
|
||
11 years ago
|
*
|
||
|
* ```php
|
||
|
* [
|
||
|
* 'offset' => 'col-sm-offset-3',
|
||
|
* 'label' => 'col-sm-3',
|
||
|
* 'wrapper' => 'col-sm-6',
|
||
|
* 'error' => '',
|
||
|
* 'hint' => 'col-sm-3',
|
||
|
* ]
|
||
|
* ```
|
||
|
*
|
||
|
* To get a different column layout in horizontal mode you can modify those options
|
||
|
* through [[fieldConfig]]:
|
||
|
*
|
||
|
* ```php
|
||
|
* $form = ActiveForm::begin([
|
||
|
* 'layout' => 'horizontal',
|
||
|
* 'fieldConfig' => [
|
||
|
* 'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
|
||
|
* 'horizontalCssClasses' => [
|
||
|
* 'label' => 'col-sm-4',
|
||
|
* 'offset' => 'col-sm-offset-4',
|
||
|
* 'wrapper' => 'col-sm-8',
|
||
|
* 'error' => '',
|
||
|
* 'hint' => '',
|
||
|
* ],
|
||
|
* ],
|
||
|
* ]);
|
||
|
* ```
|
||
|
*
|
||
9 years ago
|
* @see ActiveField for details on the [[fieldConfig]] options
|
||
11 years ago
|
* @see http://getbootstrap.com/css/#forms
|
||
|
*
|
||
|
* @author Michael Härtl <haertl.mike@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class ActiveForm extends \yii\widgets\ActiveForm
|
||
|
{
|
||
|
/**
|
||
10 years ago
|
* @var string the default field class name when calling [[field()]] to create a new field.
|
||
|
* @see fieldConfig
|
||
|
*/
|
||
|
public $fieldClass = 'yii\bootstrap\ActiveField';
|
||
|
/**
|
||
8 years ago
|
* @var array HTML attributes for the form tag. Default is `[]`.
|
||
11 years ago
|
*/
|
||
8 years ago
|
public $options = [];
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @var string the form layout. Either 'default', 'horizontal' or 'inline'.
|
||
|
* By choosing a layout, an appropriate default field configuration is applied. This will
|
||
11 years ago
|
* 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
|
||
|
*/
|
||
11 years ago
|
public $layout = 'default';
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
7 years ago
|
* {@inheritdoc}
|
||
11 years ago
|
*/
|
||
|
public function init()
|
||
|
{
|
||
11 years ago
|
if (!in_array($this->layout, ['default', 'horizontal', 'inline'])) {
|
||
|
throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
if ($this->layout !== 'default') {
|
||
|
Html::addCssClass($this->options, 'form-' . $this->layout);
|
||
11 years ago
|
}
|
||
11 years ago
|
parent::init();
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
/**
|
||
7 years ago
|
* {@inheritdoc}
|
||
10 years ago
|
* @return ActiveField the created ActiveField object
|
||
|
*/
|
||
|
public function field($model, $attribute, $options = [])
|
||
|
{
|
||
|
return parent::field($model, $attribute, $options);
|
||
|
}
|
||
11 years ago
|
}
|