ActiveForm.php 4.04 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 35 36 37 38
	public $fieldOptions = array('tag' => 'div', 'class' => 'yii-field');
	public $fieldTemplate = "{label}\n{input}\n{error}";
	public $autoFieldCssClass = true;
	public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
	public $labelOptions = array('class' => 'control-label');
Qiang Xue committed
39
	/**
Qiang Xue committed
40 41
	 * @var string the default CSS class for the error summary container.
	 * @see errorSummary()
Qiang Xue committed
42
	 */
Qiang Xue committed
43
	public $errorSummaryCssClass = 'yii-error-summary';
Qiang Xue committed
44 45 46 47
	/**
	 * @var string the default CSS class that indicates an input is required.
	 */
	public $requiredCssClass = 'required';
Qiang Xue committed
48
	/**
Qiang Xue committed
49
	 * @var string the default CSS class that indicates an input has error.
Qiang Xue committed
50
	 */
Qiang Xue committed
51
	public $errorCssClass = 'error';
Alexander Makarov committed
52 53 54
	/**
	 * @var string the default CSS class that indicates an input validated successfully.
	 */
Qiang Xue committed
55
	public $successCssClass = 'success';
Alexander Makarov committed
56 57 58
	/**
	 * @var string the default CSS class that indicates an input is currently being validated.
	 */
Qiang Xue committed
59
	public $validatingCssClass = 'validating';
Qiang Xue committed
60 61 62 63 64 65 66
	/**
	 * @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
67
	public $fieldClass = 'yii\widgets\ActiveField';
Qiang Xue committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	/**
	 * 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
86 87 88 89 90 91
	/**
	 * @param Model|Model[] $models
	 * @param array $options
	 * @return string
	 */
	public function errorSummary($models, $options = array())
Qiang Xue committed
92
	{
Qiang Xue committed
93 94 95 96
		if (!is_array($models)) {
			$models = array($models);
		}

Alexander Makarov committed
97
		$showAll = !empty($options['showAll']);
Qiang Xue committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
		$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
112
		$tag = isset($options['tag']) ? $options['tag'] : 'div';
Qiang Xue committed
113 114 115
		unset($options['showAll'], $options['header'], $options['footer'], $options['container']);

		if (!isset($options['class'])) {
Qiang Xue committed
116
			$options['class'] = $this->errorSummaryCssClass;
Qiang Xue committed
117
		} else {
Qiang Xue committed
118
			$options['class'] .= ' ' . $this->errorSummaryCssClass;
Qiang Xue committed
119 120 121 122
		}

		if ($lines !== array()) {
			$content = "<ul><li>" . implode("</li>\n<li>", ArrayHelper::htmlEncode($lines)) . "</li><ul>";
Qiang Xue committed
123
			return Html::tag($tag, $header . $content . $footer, $options);
Qiang Xue committed
124 125 126
		} else {
			$content = "<ul></ul>";
			$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
Qiang Xue committed
127
			return Html::tag($tag, $header . $content . $footer, $options);
Qiang Xue committed
128
		}
Qiang Xue committed
129 130
	}

Qiang Xue committed
131
	public function field($model, $attribute, $options = null)
Qiang Xue committed
132
	{
Qiang Xue committed
133
		return Yii::createObject(array(
Qiang Xue committed
134 135 136 137 138 139
			'class' => $this->fieldClass,
			'model' => $model,
			'attribute' => $attribute,
			'form' => $this,
			'options' => $options,
		));
Qiang Xue committed
140
	}
Qiang Xue committed
141
}