ActiveForm.php 5.05 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 15 16 17 18 19 20 21

/**
 * ActiveForm ...
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveForm extends Widget
Qiang Xue committed
22
{
Qiang Xue committed
23
	/**
Qiang Xue committed
24
	 * @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Html::url()]].
Qiang Xue committed
25 26 27 28 29 30 31
	 */
	public $action = '';
	/**
	 * @var string the form submission method. This should be either 'post' or 'get'.
	 * Defaults to 'post'.
	 */
	public $method = 'post';
Qiang Xue committed
32
	/**
Qiang Xue committed
33 34
	 * @var array the HTML attributes (name-value pairs) for the form tag.
	 * The values will be HTML-encoded using [[Html::encode()]].
Qiang Xue committed
35 36
	 * If a value is null, the corresponding attribute will not be rendered.
	 */
Qiang Xue committed
37
	public $options = array();
Qiang Xue committed
38
	/**
Qiang Xue committed
39 40
	 * @var string the default CSS class for the error summary container.
	 * @see errorSummary()
Qiang Xue committed
41
	 */
Qiang Xue committed
42
	public $errorSummaryCssClass = 'yii-error-summary';
Qiang Xue committed
43
	/**
Qiang Xue committed
44 45 46 47 48 49 50
	 * @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.
Qiang Xue committed
51
	 */
Qiang Xue committed
52 53 54
	public $fieldConfig = array(
		'class' => 'yii\widgets\ActiveField',
	);
Qiang Xue committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	/**
	 * @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
71

Qiang Xue committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	/**
	 * 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
90
	/**
Qiang Xue committed
91 92 93 94 95 96 97 98 99 100 101
	 * 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
102 103
	 */
	public function errorSummary($models, $options = array())
Qiang Xue committed
104
	{
Qiang Xue committed
105 106 107 108 109 110
		if (!is_array($models)) {
			$models = array($models);
		}

		$lines = array();
		foreach ($models as $model) {
Qiang Xue committed
111 112 113
			/** @var $model Model */
			foreach ($model->getFirstErrors() as $error) {
				$lines[] = Html::encode($error);
Qiang Xue committed
114 115 116 117 118
			}
		}

		$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
119
		unset($options['header'], $options['footer']);
Qiang Xue committed
120 121

		if (!isset($options['class'])) {
Qiang Xue committed
122
			$options['class'] = $this->errorSummaryCssClass;
Qiang Xue committed
123
		} else {
Qiang Xue committed
124
			$options['class'] .= ' ' . $this->errorSummaryCssClass;
Qiang Xue committed
125 126 127
		}

		if ($lines !== array()) {
Qiang Xue committed
128 129
			$content = "<ul><li>" . implode("</li>\n<li>", $lines) . "</li><ul>";
			return Html::tag('div', $header . $content . $footer, $options);
Qiang Xue committed
130 131 132
		} else {
			$content = "<ul></ul>";
			$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
Qiang Xue committed
133
			return Html::tag('div', $header . $content . $footer, $options);
Qiang Xue committed
134
		}
Qiang Xue committed
135 136
	}

Qiang Xue committed
137 138 139 140 141 142 143 144 145 146
	/**
	 * 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.
	 * @return ActiveField the created ActiveField object
	 */
	public function field($model, $attribute, $options = array())
Qiang Xue committed
147
	{
Qiang Xue committed
148
		return Yii::createObject(array_merge($this->fieldConfig, $options, array(
Qiang Xue committed
149 150 151
			'model' => $model,
			'attribute' => $attribute,
			'form' => $this,
Qiang Xue committed
152
		)));
Qiang Xue committed
153
	}
Qiang Xue committed
154
}