ActiveForm.php 7.86 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
use yii\helpers\Html;
Qiang Xue committed
14
use yii\helpers\Json;
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
	/**
Qiang Xue committed
34 35
	 * @var array the HTML attributes (name-value pairs) for the form tag.
	 * The values will be HTML-encoded using [[Html::encode()]].
Qiang Xue committed
36 37
	 * If a value is null, the corresponding attribute will not be rendered.
	 */
Qiang Xue committed
38
	public $options = array();
Qiang Xue committed
39 40
	/**
	 * @var array the default configuration used by [[field()]] when creating a new field object.
Qiang Xue committed
41
	 */
42
	public $fieldConfig;
Qiang Xue committed
43
	/**
Qiang Xue committed
44 45
	 * @var string the default CSS class for the error summary container.
	 * @see errorSummary()
Qiang Xue committed
46
	 */
Qiang Xue committed
47
	public $errorSummaryCssClass = 'error-summary';
Qiang Xue committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	/**
	 * @var string the CSS class that is added to a field container when the associated attribute is required.
	 */
	public $requiredCssClass = 'required';
	/**
	 * @var string the CSS class that is added to a field container when the associated attribute has validation error.
	 */
	public $errorCssClass = 'error';
	/**
	 * @var string the CSS class that is added to a field container when the associated attribute is successfully validated.
	 */
	public $successCssClass = 'success';
	/**
	 * @var string the CSS class that is added to a field container when the associated attribute is being validated.
	 */
	public $validatingCssClass = 'validating';
Qiang Xue committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	/**
	 * @var boolean whether to enable client-side data validation.
	 * If [[ActiveField::enableClientValidation]] is set, its value will take precedence for that input field.
	 */
	public $enableClientValidation = true;
	/**
	 * @var boolean whether to enable AJAX-based data validation.
	 * If [[ActiveField::enableAjaxValidation]] is set, its value will take precedence for that input field.
	 */
	public $enableAjaxValidation = false;
	/**
	 * @var array|string the URL for performing AJAX-based validation. This property will be processed by
	 * [[Html::url()]]. Please refer to [[Html::url()]] for more details on how to configure this property.
	 * If this property is not set, it will take the value of the form's action attribute.
	 */
Qiang Xue committed
79
	public $validationUrl;
Qiang Xue committed
80 81 82 83 84 85 86 87
	/**
	 * @var boolean whether to perform validation when the form is submitted.
	 */
	public $validateOnSubmit = true;
	/**
	 * @var boolean whether to perform validation when an input field loses focus and its value is found changed.
	 * If [[ActiveField::validateOnChange]] is set, its value will take precedence for that input field.
	 */
Qiang Xue committed
88
	public $validateOnChange = true;
Qiang Xue committed
89 90 91 92 93 94 95
	/**
	 * @var boolean whether to perform validation while the user is typing in an input field.
	 * If [[ActiveField::validateOnType]] is set, its value will take precedence for that input field.
	 * @see validationDelay
	 */
	public $validateOnType = false;
	/**
Qiang Xue committed
96 97
	 * @var integer number of milliseconds that the validation should be delayed when an input field
	 * is changed or the user types in the field.
Qiang Xue committed
98 99 100
	 * If [[ActiveField::validationDelay]] is set, its value will take precedence for that input field.
	 */
	public $validationDelay = 200;
Qiang Xue committed
101 102 103 104
	/**
	 * @var string the name of the GET parameter indicating the validation request is an AJAX request.
	 */
	public $ajaxVar = 'ajax';
Qiang Xue committed
105
	/**
Qiang Xue committed
106
	 * @var array the client validation options for individual attributes. Each element of the array
Qiang Xue committed
107 108 109
	 * represents the validation options for a particular attribute.
	 * @internal
	 */
Qiang Xue committed
110 111
	public $attributes = array();

Qiang Xue committed
112 113 114 115 116 117
	/**
	 * Initializes the widget.
	 * This renders the form open tag.
	 */
	public function init()
	{
Qiang Xue committed
118 119 120
		if (!isset($this->options['id'])) {
			$this->options['id'] = $this->getId();
		}
121 122 123
		if (!isset($this->fieldConfig['class'])) {
			$this->fieldConfig['class'] = 'yii\widgets\ActiveField';
		}
Qiang Xue committed
124 125 126 127 128 129 130 131 132
		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()
	{
133
		if (!empty($this->attributes)) {
Qiang Xue committed
134 135 136
			$id = $this->options['id'];
			$options = Json::encode($this->getClientOptions());
			$attributes = Json::encode($this->attributes);
137 138
			$this->getView()->registerAssetBundle('yii/form');
			$this->getView()->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
Qiang Xue committed
139 140 141 142
		}
		echo Html::endForm();
	}

Qiang Xue committed
143 144 145 146
	/**
	 * Returns the options for the form JS widget.
	 * @return array the options
	 */
Qiang Xue committed
147 148
	protected function getClientOptions()
	{
Qiang Xue committed
149
		$options = array(
Qiang Xue committed
150 151
			'errorSummary' => '.' . $this->errorSummaryCssClass,
			'validateOnSubmit' => $this->validateOnSubmit,
Qiang Xue committed
152 153 154
			'errorCssClass' => $this->errorCssClass,
			'successCssClass' => $this->successCssClass,
			'validatingCssClass' => $this->validatingCssClass,
Qiang Xue committed
155
			'ajaxVar' => $this->ajaxVar,
Qiang Xue committed
156
		);
Qiang Xue committed
157 158 159 160
		if ($this->validationUrl !== null) {
			$options['validationUrl'] = Html::url($this->validationUrl);
		}
		return $options;
Qiang Xue committed
161 162
	}

Qiang Xue committed
163
	/**
Qiang Xue committed
164 165 166 167 168 169 170 171 172 173 174
	 * Generates a summary of the validation errors.
	 * If there is no validation error, an empty error summary markup will still be generated, but it will be hidden.
	 * @param Model|Model[] $models the model(s) associated with this form
	 * @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
	 *
	 * - header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
	 * - footer: string, the footer HTML for the error summary.
	 *
	 * The rest of the options will be rendered as the attributes of the container tag. The values will
	 * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
	 * @return string the generated error summary
Qiang Xue committed
175 176
	 */
	public function errorSummary($models, $options = array())
Qiang Xue committed
177
	{
Qiang Xue committed
178 179 180 181 182 183
		if (!is_array($models)) {
			$models = array($models);
		}

		$lines = array();
		foreach ($models as $model) {
Qiang Xue committed
184 185 186
			/** @var $model Model */
			foreach ($model->getFirstErrors() as $error) {
				$lines[] = Html::encode($error);
Qiang Xue committed
187 188 189
			}
		}

190
		$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
Qiang Xue committed
191
		$footer = isset($options['footer']) ? $options['footer'] : '';
Qiang Xue committed
192
		unset($options['header'], $options['footer']);
Qiang Xue committed
193 194

		if (!isset($options['class'])) {
Qiang Xue committed
195
			$options['class'] = $this->errorSummaryCssClass;
Qiang Xue committed
196
		} else {
Qiang Xue committed
197
			$options['class'] .= ' ' . $this->errorSummaryCssClass;
Qiang Xue committed
198 199
		}

200
		if (!empty($lines)) {
Qiang Xue committed
201 202
			$content = "<ul><li>" . implode("</li>\n<li>", $lines) . "</li><ul>";
			return Html::tag('div', $header . $content . $footer, $options);
Qiang Xue committed
203 204 205
		} else {
			$content = "<ul></ul>";
			$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
Qiang Xue committed
206
			return Html::tag('div', $header . $content . $footer, $options);
Qiang Xue committed
207
		}
Qiang Xue committed
208 209
	}

Qiang Xue committed
210 211 212 213 214 215 216
	/**
	 * Generates a form field.
	 * A form field is associated with a model and an attribute. It contains a label, an input and an error message
	 * and use them to interact with end users to collect their inputs for the attribute.
	 * @param Model $model the data model
	 * @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
	 * about attribute expression.
Qiang Xue committed
217
	 * @param array $options the additional configurations for the field object
Qiang Xue committed
218
	 * @return ActiveField the created ActiveField object
Qiang Xue committed
219
	 * @see fieldConfig
Qiang Xue committed
220 221
	 */
	public function field($model, $attribute, $options = array())
Qiang Xue committed
222
	{
Qiang Xue committed
223
		return Yii::createObject(array_merge($this->fieldConfig, $options, array(
Qiang Xue committed
224 225 226
			'model' => $model,
			'attribute' => $attribute,
			'form' => $this,
Qiang Xue committed
227
		)));
Qiang Xue committed
228
	}
Qiang Xue committed
229
}