1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\widgets; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidCallException; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\base\Widget; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
use yii\helpers\Html; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
use yii\helpers\Url; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ActiveForm is a widget that builds an interactive HTML form for one or multiple data models. |
21
|
|
|
* |
22
|
|
|
* For more details and usage information on ActiveForm, see the [guide article on forms](guide:input-forms). |
23
|
|
|
* |
24
|
|
|
* @author Qiang Xue <[email protected]> |
25
|
|
|
* @since 2.0 |
26
|
|
|
*/ |
27
|
|
|
class ActiveForm extends Widget |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Add validation state class to container tag |
31
|
|
|
* @since 2.0.14 |
32
|
|
|
*/ |
33
|
|
|
const VALIDATION_STATE_ON_CONTAINER = 'container'; |
34
|
|
|
/** |
35
|
|
|
* Add validation state class to input tag |
36
|
|
|
* @since 2.0.14 |
37
|
|
|
*/ |
38
|
|
|
const VALIDATION_STATE_ON_INPUT = 'input'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array|string the form action URL. This parameter will be processed by [[\yii\helpers\Url::to()]]. |
42
|
|
|
* @see method for specifying the HTTP method for this form. |
43
|
|
|
*/ |
44
|
|
|
public $action = ''; |
45
|
|
|
/** |
46
|
|
|
* @var string the form submission method. This should be either `post` or `get`. Defaults to `post`. |
47
|
|
|
* |
48
|
|
|
* When you set this to `get` you may see the url parameters repeated on each request. |
49
|
|
|
* This is because the default value of [[action]] is set to be the current request url and each submit |
50
|
|
|
* will add new parameters instead of replacing existing ones. |
51
|
|
|
* You may set [[action]] explicitly to avoid this: |
52
|
|
|
* |
53
|
|
|
* ```php |
54
|
|
|
* $form = ActiveForm::begin([ |
55
|
|
|
* 'method' => 'get', |
56
|
|
|
* 'action' => ['controller/action'], |
57
|
|
|
* ]); |
58
|
|
|
* ``` |
59
|
|
|
*/ |
60
|
|
|
public $method = 'post'; |
61
|
|
|
/** |
62
|
|
|
* @var array the HTML attributes (name-value pairs) for the form tag. |
63
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
64
|
|
|
*/ |
65
|
|
|
public $options = []; |
66
|
|
|
/** |
67
|
|
|
* @var string the default field class name when calling [[field()]] to create a new field. |
68
|
|
|
* @see fieldConfig |
69
|
|
|
*/ |
70
|
|
|
public $fieldClass = 'yii\widgets\ActiveField'; |
71
|
|
|
/** |
72
|
|
|
* @var array|\Closure the default configuration used by [[field()]] when creating a new field object. |
73
|
|
|
* This can be either a configuration array or an anonymous function returning a configuration array. |
74
|
|
|
* If the latter, the signature should be as follows: |
75
|
|
|
* |
76
|
|
|
* ```php |
77
|
|
|
* function ($model, $attribute) |
78
|
|
|
* ``` |
79
|
|
|
* |
80
|
|
|
* The value of this property will be merged recursively with the `$options` parameter passed to [[field()]]. |
81
|
|
|
* |
82
|
|
|
* @see fieldClass |
83
|
|
|
*/ |
84
|
|
|
public $fieldConfig = []; |
85
|
|
|
/** |
86
|
|
|
* @var bool whether to perform encoding on the error summary. |
87
|
|
|
*/ |
88
|
|
|
public $encodeErrorSummary = true; |
89
|
|
|
/** |
90
|
|
|
* @var string the default CSS class for the error summary container. |
91
|
|
|
* @see errorSummary() |
92
|
|
|
*/ |
93
|
|
|
public $errorSummaryCssClass = 'error-summary'; |
94
|
|
|
/** |
95
|
|
|
* @var string the CSS class that is added to a field container when the associated attribute is required. |
96
|
|
|
*/ |
97
|
|
|
public $requiredCssClass = 'required'; |
98
|
|
|
/** |
99
|
|
|
* @var string the CSS class that is added to a field container when the associated attribute has validation error. |
100
|
|
|
*/ |
101
|
|
|
public $errorCssClass = 'has-error'; |
102
|
|
|
/** |
103
|
|
|
* @var string the CSS class that is added to a field container when the associated attribute is successfully validated. |
104
|
|
|
*/ |
105
|
|
|
public $successCssClass = 'has-success'; |
106
|
|
|
/** |
107
|
|
|
* @var string the CSS class that is added to a field container when the associated attribute is being validated. |
108
|
|
|
*/ |
109
|
|
|
public $validatingCssClass = 'validating'; |
110
|
|
|
/** |
111
|
|
|
* @var string where to render validation state class |
112
|
|
|
* Could be either "container" or "input". |
113
|
|
|
* Default is "container". |
114
|
|
|
* @since 2.0.14 |
115
|
|
|
*/ |
116
|
|
|
public $validationStateOn = self::VALIDATION_STATE_ON_CONTAINER; |
117
|
|
|
/** |
118
|
|
|
* @var bool whether to enable client-side data validation. |
119
|
|
|
* If [[ActiveField::enableClientValidation]] is set, its value will take precedence for that input field. |
120
|
|
|
*/ |
121
|
|
|
public $enableClientValidation = true; |
122
|
|
|
/** |
123
|
|
|
* @var bool whether to enable AJAX-based data validation. |
124
|
|
|
* If [[ActiveField::enableAjaxValidation]] is set, its value will take precedence for that input field. |
125
|
|
|
*/ |
126
|
|
|
public $enableAjaxValidation = false; |
127
|
|
|
/** |
128
|
|
|
* @var bool whether to hook up `yii.activeForm` JavaScript plugin. |
129
|
|
|
* This property must be set `true` if you want to support client validation and/or AJAX validation, or if you |
130
|
|
|
* want to take advantage of the `yii.activeForm` plugin. When this is `false`, the form will not generate |
131
|
|
|
* any JavaScript. |
132
|
|
|
* @see registerClientScript |
133
|
|
|
*/ |
134
|
|
|
public $enableClientScript = true; |
135
|
|
|
/** |
136
|
|
|
* @var array|string the URL for performing AJAX-based validation. This property will be processed by |
137
|
|
|
* [[Url::to()]]. Please refer to [[Url::to()]] for more details on how to configure this property. |
138
|
|
|
* If this property is not set, it will take the value of the form's action attribute. |
139
|
|
|
*/ |
140
|
|
|
public $validationUrl; |
141
|
|
|
/** |
142
|
|
|
* @var bool whether to perform validation when the form is submitted. |
143
|
|
|
*/ |
144
|
|
|
public $validateOnSubmit = true; |
145
|
|
|
/** |
146
|
|
|
* @var bool whether to perform validation when the value of an input field is changed. |
147
|
|
|
* If [[ActiveField::validateOnChange]] is set, its value will take precedence for that input field. |
148
|
|
|
*/ |
149
|
|
|
public $validateOnChange = true; |
150
|
|
|
/** |
151
|
|
|
* @var bool whether to perform validation when an input field loses focus. |
152
|
|
|
* If [[ActiveField::$validateOnBlur]] is set, its value will take precedence for that input field. |
153
|
|
|
*/ |
154
|
|
|
public $validateOnBlur = true; |
155
|
|
|
/** |
156
|
|
|
* @var bool whether to perform validation while the user is typing in an input field. |
157
|
|
|
* If [[ActiveField::validateOnType]] is set, its value will take precedence for that input field. |
158
|
|
|
* @see validationDelay |
159
|
|
|
*/ |
160
|
|
|
public $validateOnType = false; |
161
|
|
|
/** |
162
|
|
|
* @var int number of milliseconds that the validation should be delayed when the user types in the field |
163
|
|
|
* and [[validateOnType]] is set `true`. |
164
|
|
|
* If [[ActiveField::validationDelay]] is set, its value will take precedence for that input field. |
165
|
|
|
*/ |
166
|
|
|
public $validationDelay = 500; |
167
|
|
|
/** |
168
|
|
|
* @var string the name of the GET parameter indicating the validation request is an AJAX request. |
169
|
|
|
*/ |
170
|
|
|
public $ajaxParam = 'ajax'; |
171
|
|
|
/** |
172
|
|
|
* @var string the type of data that you're expecting back from the server. |
173
|
|
|
*/ |
174
|
|
|
public $ajaxDataType = 'json'; |
175
|
|
|
/** |
176
|
|
|
* @var bool whether to scroll to the first error after validation. |
177
|
|
|
* @since 2.0.6 |
178
|
|
|
*/ |
179
|
|
|
public $scrollToError = true; |
180
|
|
|
/** |
181
|
|
|
* @var int offset in pixels that should be added when scrolling to the first error. |
182
|
|
|
* @since 2.0.11 |
183
|
|
|
*/ |
184
|
|
|
public $scrollToErrorOffset = 0; |
185
|
|
|
/** |
186
|
|
|
* @var array the client validation options for individual attributes. Each element of the array |
187
|
|
|
* represents the validation options for a particular attribute. |
188
|
|
|
* @internal |
189
|
|
|
*/ |
190
|
|
|
public $attributes = []; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @var ActiveField[] the ActiveField objects that are currently active |
194
|
|
|
*/ |
195
|
|
|
private $_fields = []; |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Initializes the widget. |
200
|
|
|
* This renders the form open tag. |
201
|
|
|
*/ |
202
|
38 |
|
public function init() |
203
|
|
|
{ |
204
|
38 |
|
parent::init(); |
205
|
38 |
|
if (!isset($this->options['id'])) { |
206
|
38 |
|
$this->options['id'] = $this->getId(); |
207
|
|
|
} |
208
|
38 |
|
ob_start(); |
209
|
38 |
|
ob_implicit_flush(false); |
|
|
|
|
210
|
38 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Runs the widget. |
214
|
|
|
* This registers the necessary JavaScript code and renders the form open and close tags. |
215
|
|
|
* @throws InvalidCallException if `beginField()` and `endField()` calls are not matching. |
216
|
|
|
*/ |
217
|
38 |
|
public function run() |
218
|
|
|
{ |
219
|
38 |
|
if (!empty($this->_fields)) { |
220
|
|
|
throw new InvalidCallException('Each beginField() should have a matching endField() call.'); |
221
|
|
|
} |
222
|
|
|
|
223
|
38 |
|
$content = ob_get_clean(); |
224
|
38 |
|
$html = Html::beginForm($this->action, $this->method, $this->options); |
225
|
38 |
|
$html .= $content; |
226
|
|
|
|
227
|
38 |
|
if ($this->enableClientScript) { |
228
|
|
|
$this->registerClientScript(); |
229
|
|
|
} |
230
|
|
|
|
231
|
38 |
|
$html .= Html::endForm(); |
232
|
38 |
|
return $html; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* This registers the necessary JavaScript code. |
237
|
|
|
* @since 2.0.12 |
238
|
|
|
*/ |
239
|
|
|
public function registerClientScript() |
240
|
|
|
{ |
241
|
|
|
$id = $this->options['id']; |
242
|
|
|
$options = Json::htmlEncode($this->getClientOptions()); |
243
|
|
|
$attributes = Json::htmlEncode($this->attributes); |
244
|
|
|
$view = $this->getView(); |
245
|
|
|
ActiveFormAsset::register($view); |
246
|
|
|
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);"); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns the options for the form JS widget. |
251
|
|
|
* @return array the options. |
252
|
|
|
*/ |
253
|
|
|
protected function getClientOptions() |
254
|
|
|
{ |
255
|
|
|
$options = [ |
256
|
|
|
'encodeErrorSummary' => $this->encodeErrorSummary, |
257
|
|
|
'errorSummary' => '.' . implode('.', preg_split('/\s+/', $this->errorSummaryCssClass, -1, PREG_SPLIT_NO_EMPTY)), |
|
|
|
|
258
|
|
|
'validateOnSubmit' => $this->validateOnSubmit, |
259
|
|
|
'errorCssClass' => $this->errorCssClass, |
260
|
|
|
'successCssClass' => $this->successCssClass, |
261
|
|
|
'validatingCssClass' => $this->validatingCssClass, |
262
|
|
|
'ajaxParam' => $this->ajaxParam, |
263
|
|
|
'ajaxDataType' => $this->ajaxDataType, |
264
|
|
|
'scrollToError' => $this->scrollToError, |
265
|
|
|
'scrollToErrorOffset' => $this->scrollToErrorOffset, |
266
|
|
|
'validationStateOn' => $this->validationStateOn, |
267
|
|
|
]; |
268
|
|
|
if ($this->validationUrl !== null) { |
269
|
|
|
$options['validationUrl'] = Url::to($this->validationUrl); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// only get the options that are different from the default ones (set in yii.activeForm.js) |
273
|
|
|
return array_diff_assoc($options, [ |
274
|
|
|
'encodeErrorSummary' => true, |
275
|
|
|
'errorSummary' => '.error-summary', |
276
|
|
|
'validateOnSubmit' => true, |
277
|
|
|
'errorCssClass' => 'has-error', |
278
|
|
|
'successCssClass' => 'has-success', |
279
|
|
|
'validatingCssClass' => 'validating', |
280
|
|
|
'ajaxParam' => 'ajax', |
281
|
|
|
'ajaxDataType' => 'json', |
282
|
|
|
'scrollToError' => true, |
283
|
|
|
'scrollToErrorOffset' => 0, |
284
|
|
|
'validationStateOn' => self::VALIDATION_STATE_ON_CONTAINER, |
285
|
|
|
]); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Generates a summary of the validation errors. |
290
|
|
|
* If there is no validation error, an empty error summary markup will still be generated, but it will be hidden. |
291
|
|
|
* @param Model|Model[] $models the model(s) associated with this form. |
292
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
293
|
|
|
* |
294
|
|
|
* - `header`: string, the header HTML for the error summary. If not set, a default prompt string will be used. |
295
|
|
|
* - `footer`: string, the footer HTML for the error summary. |
296
|
|
|
* |
297
|
|
|
* The rest of the options will be rendered as the attributes of the container tag. The values will |
298
|
|
|
* be HTML-encoded using [[\yii\helpers\Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered. |
299
|
|
|
* @return string the generated error summary. |
300
|
|
|
* @see errorSummaryCssClass |
301
|
|
|
*/ |
302
|
|
|
public function errorSummary($models, $options = []) |
303
|
|
|
{ |
304
|
|
|
Html::addCssClass($options, $this->errorSummaryCssClass); |
305
|
|
|
$options['encode'] = $this->encodeErrorSummary; |
306
|
|
|
return Html::errorSummary($models, $options); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Generates a form field. |
311
|
|
|
* A form field is associated with a model and an attribute. It contains a label, an input and an error message |
312
|
|
|
* and use them to interact with end users to collect their inputs for the attribute. |
313
|
|
|
* @param Model $model the data model. |
314
|
|
|
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format |
315
|
|
|
* about attribute expression. |
316
|
|
|
* @param array $options the additional configurations for the field object. These are properties of [[ActiveField]] |
317
|
|
|
* or a subclass, depending on the value of [[fieldClass]]. |
318
|
|
|
* @return ActiveField the created ActiveField object. |
319
|
|
|
* @see fieldConfig |
320
|
|
|
*/ |
321
|
4 |
|
public function field($model, $attribute, $options = []) |
322
|
|
|
{ |
323
|
4 |
|
$config = $this->fieldConfig; |
324
|
4 |
|
if ($config instanceof \Closure) { |
325
|
|
|
$config = call_user_func($config, $model, $attribute); |
326
|
|
|
} |
327
|
4 |
|
if (!isset($config['class'])) { |
328
|
4 |
|
$config['class'] = $this->fieldClass; |
329
|
|
|
} |
330
|
|
|
|
331
|
4 |
|
return Yii::createObject(ArrayHelper::merge($config, $options, [ |
332
|
4 |
|
'model' => $model, |
333
|
4 |
|
'attribute' => $attribute, |
334
|
4 |
|
'form' => $this, |
335
|
|
|
])); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Begins a form field. |
340
|
|
|
* This method will create a new form field and returns its opening tag. |
341
|
|
|
* You should call [[endField()]] afterwards. |
342
|
|
|
* @param Model $model the data model. |
343
|
|
|
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format |
344
|
|
|
* about attribute expression. |
345
|
|
|
* @param array $options the additional configurations for the field object. |
346
|
|
|
* @return string the opening tag. |
347
|
|
|
* @see endField() |
348
|
|
|
* @see field() |
349
|
|
|
*/ |
350
|
|
|
public function beginField($model, $attribute, $options = []) |
351
|
|
|
{ |
352
|
|
|
$field = $this->field($model, $attribute, $options); |
353
|
|
|
$this->_fields[] = $field; |
354
|
|
|
return $field->begin(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Ends a form field. |
359
|
|
|
* This method will return the closing tag of an active form field started by [[beginField()]]. |
360
|
|
|
* @return string the closing tag of the form field. |
361
|
|
|
* @throws InvalidCallException if this method is called without a prior [[beginField()]] call. |
362
|
|
|
*/ |
363
|
|
|
public function endField() |
364
|
|
|
{ |
365
|
|
|
$field = array_pop($this->_fields); |
366
|
|
|
if ($field instanceof ActiveField) { |
367
|
|
|
return $field->end(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
throw new InvalidCallException('Mismatching endField() call.'); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Validates one or several models and returns an error message array indexed by the attribute IDs. |
375
|
|
|
* This is a helper method that simplifies the way of writing AJAX validation code. |
376
|
|
|
* |
377
|
|
|
* For example, you may use the following code in a controller action to respond |
378
|
|
|
* to an AJAX validation request: |
379
|
|
|
* |
380
|
|
|
* ```php |
381
|
|
|
* $model = new Post; |
382
|
|
|
* $model->load(Yii::$app->request->post()); |
383
|
|
|
* if (Yii::$app->request->isAjax) { |
384
|
|
|
* Yii::$app->response->format = Response::FORMAT_JSON; |
385
|
|
|
* return ActiveForm::validate($model); |
386
|
|
|
* } |
387
|
|
|
* // ... respond to non-AJAX request ... |
388
|
|
|
* ``` |
389
|
|
|
* |
390
|
|
|
* To validate multiple models, simply pass each model as a parameter to this method, like |
391
|
|
|
* the following: |
392
|
|
|
* |
393
|
|
|
* ```php |
394
|
|
|
* ActiveForm::validate($model1, $model2, ...); |
395
|
|
|
* ``` |
396
|
|
|
* |
397
|
|
|
* @param Model $model the model to be validated. |
398
|
|
|
* @param mixed $attributes list of attributes that should be validated. |
399
|
|
|
* If this parameter is empty, it means any attribute listed in the applicable |
400
|
|
|
* validation rules should be validated. |
401
|
|
|
* |
402
|
|
|
* When this method is used to validate multiple models, this parameter will be interpreted |
403
|
|
|
* as a model. |
404
|
|
|
* |
405
|
|
|
* @return array the error message array indexed by the attribute IDs. |
406
|
|
|
*/ |
407
|
|
|
public static function validate($model, $attributes = null) |
408
|
|
|
{ |
409
|
|
|
$result = []; |
410
|
|
|
if ($attributes instanceof Model) { |
411
|
|
|
// validating multiple models |
412
|
|
|
$models = func_get_args(); |
413
|
|
|
$attributes = null; |
414
|
|
|
} else { |
415
|
|
|
$models = [$model]; |
416
|
|
|
} |
417
|
|
|
/* @var $model Model */ |
418
|
|
|
foreach ($models as $model) { |
|
|
|
|
419
|
|
|
$model->validate($attributes); |
420
|
|
|
foreach ($model->getErrors() as $attribute => $errors) { |
421
|
|
|
$result[Html::getInputId($model, $attribute)] = $errors; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $result; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Validates an array of model instances and returns an error message array indexed by the attribute IDs. |
430
|
|
|
* This is a helper method that simplifies the way of writing AJAX validation code for tabular input. |
431
|
|
|
* |
432
|
|
|
* For example, you may use the following code in a controller action to respond |
433
|
|
|
* to an AJAX validation request: |
434
|
|
|
* |
435
|
|
|
* ```php |
436
|
|
|
* // ... load $models ... |
437
|
|
|
* if (Yii::$app->request->isAjax) { |
438
|
|
|
* Yii::$app->response->format = Response::FORMAT_JSON; |
439
|
|
|
* return ActiveForm::validateMultiple($models); |
440
|
|
|
* } |
441
|
|
|
* // ... respond to non-AJAX request ... |
442
|
|
|
* ``` |
443
|
|
|
* |
444
|
|
|
* @param array $models an array of models to be validated. |
445
|
|
|
* @param mixed $attributes list of attributes that should be validated. |
446
|
|
|
* If this parameter is empty, it means any attribute listed in the applicable |
447
|
|
|
* validation rules should be validated. |
448
|
|
|
* @return array the error message array indexed by the attribute IDs. |
449
|
|
|
*/ |
450
|
|
|
public static function validateMultiple($models, $attributes = null) |
451
|
|
|
{ |
452
|
|
|
$result = []; |
453
|
|
|
/* @var $model Model */ |
454
|
|
|
foreach ($models as $i => $model) { |
455
|
|
|
$model->validate($attributes); |
456
|
|
|
foreach ($model->getErrors() as $attribute => $errors) { |
457
|
|
|
$result[Html::getInputId($model, "[$i]" . $attribute)] = $errors; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return $result; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|