Yii2 Bootstrap 3
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.

130 lines
3.5 KiB

<?php
12 years ago
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
12 years ago
namespace yii\widgets;
12 years ago
use Yii;
12 years ago
use yii\base\Widget;
12 years ago
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
12 years ago
/**
* ActiveForm ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveForm extends Widget
{
12 years ago
/**
* @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Html::url()]].
12 years ago
*/
public $action = '';
/**
* @var string the form submission method. This should be either 'post' or 'get'.
* Defaults to 'post'.
*/
public $method = 'post';
12 years ago
/**
* @param array $options the attributes (name-value pairs) for the form tag.
* The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
*/
public $options = array();
12 years ago
/**
12 years ago
* @var string the default CSS class for the error summary container.
* @see errorSummary()
12 years ago
*/
public $errorSummaryCssClass = 'yii-error-summary';
12 years ago
/**
12 years ago
* @var boolean whether to enable client-side data validation.
* Client-side validation will be performed by validators that support it
* (see [[\yii\validators\Validator::enableClientValidation]] and [[\yii\validators\Validator::clientValidateAttribute()]]).
*/
public $enableClientValidation = true;
/**
* @var array the default configuration used by [[field()]] when creating a new field object.
12 years ago
*/
12 years ago
public $fieldConfig = array(
'class' => 'yii\widgets\ActiveField',
);
12 years ago
/**
* Initializes the widget.
* This renders the form open tag.
*/
public function init()
{
echo Html::beginForm($this->action, $this->method, $this->options);
}
/**
* Runs the widget.
* This registers the necessary javascript code and renders the form close tag.
*/
public function run()
{
echo Html::endForm();
}
12 years ago
/**
* @param Model|Model[] $models
* @param array $options
* @return string
*/
public function errorSummary($models, $options = array())
12 years ago
{
12 years ago
if (!is_array($models)) {
$models = array($models);
}
$showAll = !empty($options['showAll']);
12 years ago
$lines = array();
/** @var $model Model */
foreach ($models as $model) {
if ($showAll) {
foreach ($model->getErrors() as $errors) {
$lines = array_merge($lines, $errors);
}
} else {
$lines = array_merge($lines, $model->getFirstErrors());
}
}
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii|Please fix the following errors:') . '</p>';
$footer = isset($options['footer']) ? $options['footer'] : '';
12 years ago
$tag = isset($options['tag']) ? $options['tag'] : 'div';
12 years ago
unset($options['showAll'], $options['header'], $options['footer'], $options['container']);
if (!isset($options['class'])) {
$options['class'] = $this->errorSummaryCssClass;
12 years ago
} else {
$options['class'] .= ' ' . $this->errorSummaryCssClass;
12 years ago
}
if ($lines !== array()) {
$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
12 years ago
return Html::tag($tag, $header . $content . $footer, $options);
12 years ago
} else {
$content = "<ul></ul>";
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
12 years ago
return Html::tag($tag, $header . $content . $footer, $options);
12 years ago
}
12 years ago
}
12 years ago
public function field($model, $attribute, $options = null)
12 years ago
{
12 years ago
return Yii::createObject(array_merge($this->fieldConfig, array(
'model' => $model,
'attribute' => $attribute,
'form' => $this,
'options' => $options,
12 years ago
)));
12 years ago
}
}