ActiveForm.php 3.26 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
7

Qiang Xue committed
8 9
namespace yii\widgets;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\base\Widget;
Qiang Xue committed
12
use yii\base\Model;
Qiang Xue committed
13 14
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
Qiang Xue committed
15 16 17 18 19 20 21 22

/**
 * ActiveForm ...
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveForm extends Widget
Qiang Xue committed
23
{
Qiang Xue committed
24
	/**
Qiang Xue committed
25
	 * @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Html::url()]].
Qiang Xue committed
26 27 28 29 30 31 32
	 */
	public $action = '';
	/**
	 * @var string the form submission method. This should be either 'post' or 'get'.
	 * Defaults to 'post'.
	 */
	public $method = 'post';
Qiang Xue committed
33
	public $options = array();
Qiang Xue committed
34
	/**
Qiang Xue committed
35 36
	 * @var string the default CSS class for the error summary container.
	 * @see errorSummary()
Qiang Xue committed
37
	 */
Qiang Xue committed
38
	public $errorSummaryCssClass = 'yii-error-summary';
Qiang Xue committed
39 40 41 42 43 44 45
	/**
	 * @var boolean whether to enable client-side data validation. Defaults to false.
	 * When this property is set true, client-side validation will be performed by validators
	 * that support it (see {@link CValidator::enableClientValidation} and {@link CValidator::clientValidateAttribute}).
	 */
	public $enableClientValidation = false;

Qiang Xue committed
46 47 48 49
	public $fieldConfig = array(
		'class' => 'yii\widgets\ActiveField',
	);

Qiang Xue committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	/**
	 * 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();
	}

Qiang Xue committed
68 69 70 71 72 73
	/**
	 * @param Model|Model[] $models
	 * @param array $options
	 * @return string
	 */
	public function errorSummary($models, $options = array())
Qiang Xue committed
74
	{
Qiang Xue committed
75 76 77 78
		if (!is_array($models)) {
			$models = array($models);
		}

Alexander Makarov committed
79
		$showAll = !empty($options['showAll']);
Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93
		$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'] : '';
Qiang Xue committed
94
		$tag = isset($options['tag']) ? $options['tag'] : 'div';
Qiang Xue committed
95 96 97
		unset($options['showAll'], $options['header'], $options['footer'], $options['container']);

		if (!isset($options['class'])) {
Qiang Xue committed
98
			$options['class'] = $this->errorSummaryCssClass;
Qiang Xue committed
99
		} else {
Qiang Xue committed
100
			$options['class'] .= ' ' . $this->errorSummaryCssClass;
Qiang Xue committed
101 102 103 104
		}

		if ($lines !== array()) {
			$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
Qiang Xue committed
105
			return Html::tag($tag, $header . $content . $footer, $options);
Qiang Xue committed
106 107 108
		} else {
			$content = "<ul></ul>";
			$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
Qiang Xue committed
109
			return Html::tag($tag, $header . $content . $footer, $options);
Qiang Xue committed
110
		}
Qiang Xue committed
111 112
	}

Qiang Xue committed
113
	public function field($model, $attribute, $options = null)
Qiang Xue committed
114
	{
Qiang Xue committed
115
		return Yii::createObject(array_merge($this->fieldConfig, array(
Qiang Xue committed
116 117 118 119
			'model' => $model,
			'attribute' => $attribute,
			'form' => $this,
			'options' => $options,
Qiang Xue committed
120
		)));
Qiang Xue committed
121
	}
Qiang Xue committed
122
}