Complex classes like ActiveField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActiveField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ActiveField extends Component |
||
27 | { |
||
28 | /** |
||
29 | * @var ActiveForm the form that this field is associated with. |
||
30 | */ |
||
31 | public $form; |
||
32 | /** |
||
33 | * @var Model the data model that this field is associated with. |
||
34 | */ |
||
35 | public $model; |
||
36 | /** |
||
37 | * @var string the model attribute that this field is associated with. |
||
38 | */ |
||
39 | public $attribute; |
||
40 | /** |
||
41 | * @var array the HTML attributes (name-value pairs) for the field container tag. |
||
42 | * The values will be HTML-encoded using [[Html::encode()]]. |
||
43 | * If a value is `null`, the corresponding attribute will not be rendered. |
||
44 | * The following special options are recognized: |
||
45 | * |
||
46 | * - `tag`: the tag name of the container element. Defaults to `div`. Setting it to `false` will not render a container tag. |
||
47 | * See also [[\yii\helpers\Html::tag()]]. |
||
48 | * |
||
49 | * If you set a custom `id` for the container element, you may need to adjust the [[$selectors]] accordingly. |
||
50 | * |
||
51 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
52 | */ |
||
53 | public $options = ['class' => 'form-group']; |
||
54 | /** |
||
55 | * @var string the template that is used to arrange the label, the input field, the error message and the hint text. |
||
56 | * The following tokens will be replaced when [[render()]] is called: `{label}`, `{input}`, `{error}` and `{hint}`. |
||
57 | */ |
||
58 | public $template = "{label}\n{input}\n{hint}\n{error}"; |
||
59 | /** |
||
60 | * @var array the default options for the input tags. The parameter passed to individual input methods |
||
61 | * (e.g. [[textInput()]]) will be merged with this property when rendering the input tag. |
||
62 | * |
||
63 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
64 | * |
||
65 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
66 | */ |
||
67 | public $inputOptions = ['class' => 'form-control']; |
||
68 | /** |
||
69 | * @var array the default options for the error tags. The parameter passed to [[error()]] will be |
||
70 | * merged with this property when rendering the error tag. |
||
71 | * The following special options are recognized: |
||
72 | * |
||
73 | * - `tag`: the tag name of the container element. Defaults to `div`. Setting it to `false` will not render a container tag. |
||
74 | * See also [[\yii\helpers\Html::tag()]]. |
||
75 | * - `encode`: whether to encode the error output. Defaults to `true`. |
||
76 | * |
||
77 | * If you set a custom `id` for the error element, you may need to adjust the [[$selectors]] accordingly. |
||
78 | * |
||
79 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
80 | */ |
||
81 | public $errorOptions = ['class' => 'help-block']; |
||
82 | /** |
||
83 | * @var array the default options for the label tags. The parameter passed to [[label()]] will be |
||
84 | * merged with this property when rendering the label tag. |
||
85 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
86 | */ |
||
87 | public $labelOptions = ['class' => 'control-label']; |
||
88 | /** |
||
89 | * @var array the default options for the hint tags. The parameter passed to [[hint()]] will be |
||
90 | * merged with this property when rendering the hint tag. |
||
91 | * The following special options are recognized: |
||
92 | * |
||
93 | * - `tag`: the tag name of the container element. Defaults to `div`. Setting it to `false` will not render a container tag. |
||
94 | * See also [[\yii\helpers\Html::tag()]]. |
||
95 | * |
||
96 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
97 | */ |
||
98 | public $hintOptions = ['class' => 'hint-block']; |
||
99 | /** |
||
100 | * @var bool whether to enable client-side data validation. |
||
101 | * If not set, it will take the value of [[ActiveForm::enableClientValidation]]. |
||
102 | */ |
||
103 | public $enableClientValidation; |
||
104 | /** |
||
105 | * @var bool whether to enable AJAX-based data validation. |
||
106 | * If not set, it will take the value of [[ActiveForm::enableAjaxValidation]]. |
||
107 | */ |
||
108 | public $enableAjaxValidation; |
||
109 | /** |
||
110 | * @var bool whether to perform validation when the value of the input field is changed. |
||
111 | * If not set, it will take the value of [[ActiveForm::validateOnChange]]. |
||
112 | */ |
||
113 | public $validateOnChange; |
||
114 | /** |
||
115 | * @var bool whether to perform validation when the input field loses focus. |
||
116 | * If not set, it will take the value of [[ActiveForm::validateOnBlur]]. |
||
117 | */ |
||
118 | public $validateOnBlur; |
||
119 | /** |
||
120 | * @var bool whether to perform validation while the user is typing in the input field. |
||
121 | * If not set, it will take the value of [[ActiveForm::validateOnType]]. |
||
122 | * @see validationDelay |
||
123 | */ |
||
124 | public $validateOnType; |
||
125 | /** |
||
126 | * @var int number of milliseconds that the validation should be delayed when the user types in the field |
||
127 | * and [[validateOnType]] is set `true`. |
||
128 | * If not set, it will take the value of [[ActiveForm::validationDelay]]. |
||
129 | */ |
||
130 | public $validationDelay; |
||
131 | /** |
||
132 | * @var array the jQuery selectors for selecting the container, input and error tags. |
||
133 | * The array keys should be `container`, `input`, and/or `error`, and the array values |
||
134 | * are the corresponding selectors. For example, `['input' => '#my-input']`. |
||
135 | * |
||
136 | * The container selector is used under the context of the form, while the input and the error |
||
137 | * selectors are used under the context of the container. |
||
138 | * |
||
139 | * You normally do not need to set this property as the default selectors should work well for most cases. |
||
140 | */ |
||
141 | public $selectors = []; |
||
142 | /** |
||
143 | * @var array different parts of the field (e.g. input, label). This will be used together with |
||
144 | * [[template]] to generate the final field HTML code. The keys are the token names in [[template]], |
||
145 | * while the values are the corresponding HTML code. Valid tokens include `{input}`, `{label}` and `{error}`. |
||
146 | * Note that you normally don't need to access this property directly as |
||
147 | * it is maintained by various methods of this class. |
||
148 | */ |
||
149 | public $parts = []; |
||
150 | /** |
||
151 | * @var bool adds aria HTML attributes `aria-required` and `aria-invalid` for inputs |
||
152 | * @since 2.0.11 |
||
153 | */ |
||
154 | public $addAriaAttributes = true; |
||
155 | |||
156 | /** |
||
157 | * @var string this property holds a custom input id if it was set using [[inputOptions]] or in one of the |
||
158 | * `$options` parameters of the `input*` methods. |
||
159 | */ |
||
160 | private $_inputId; |
||
161 | /** |
||
162 | * @var bool if "for" field label attribute should be skipped. |
||
163 | */ |
||
164 | private $_skipLabelFor = false; |
||
165 | |||
166 | |||
167 | /** |
||
168 | * PHP magic method that returns the string representation of this object. |
||
169 | * @return string the string representation of this object. |
||
170 | */ |
||
171 | 4 | public function __toString() |
|
172 | { |
||
173 | // __toString cannot throw exception |
||
174 | // use trigger_error to bypass this limitation |
||
175 | try { |
||
176 | 4 | return $this->render(); |
|
177 | } catch (\Exception $e) { |
||
178 | ErrorHandler::convertExceptionToError($e); |
||
179 | return ''; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Renders the whole field. |
||
185 | * This method will generate the label, error tag, input tag and hint tag (if any), and |
||
186 | * assemble them into HTML according to [[template]]. |
||
187 | * @param string|callable $content the content within the field container. |
||
188 | * If `null` (not set), the default methods will be called to generate the label, error tag and input tag, |
||
189 | * and use them as the content. |
||
190 | * If a callable, it will be called to generate the content. The signature of the callable should be: |
||
191 | * |
||
192 | * ```php |
||
193 | * function ($field) { |
||
194 | * return $html; |
||
195 | * } |
||
196 | * ``` |
||
197 | * |
||
198 | * @return string the rendering result. |
||
199 | */ |
||
200 | 11 | public function render($content = null) |
|
201 | { |
||
202 | 11 | if ($content === null) { |
|
203 | 11 | if (!isset($this->parts['{input}'])) { |
|
204 | 7 | $this->textInput(); |
|
205 | } |
||
206 | 11 | if (!isset($this->parts['{label}'])) { |
|
207 | 9 | $this->label(); |
|
208 | } |
||
209 | 11 | if (!isset($this->parts['{error}'])) { |
|
210 | 9 | $this->error(); |
|
211 | } |
||
212 | 11 | if (!isset($this->parts['{hint}'])) { |
|
213 | 9 | $this->hint(null); |
|
214 | } |
||
215 | 11 | $content = strtr($this->template, $this->parts); |
|
216 | 1 | } elseif (!is_string($content)) { |
|
217 | 1 | $content = call_user_func($content, $this); |
|
218 | } |
||
219 | |||
220 | 11 | return $this->begin() . "\n" . $content . "\n" . $this->end(); |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Renders the opening tag of the field container. |
||
225 | * @return string the rendering result. |
||
226 | */ |
||
227 | 15 | public function begin() |
|
228 | { |
||
229 | 15 | if ($this->form->enableClientScript) { |
|
230 | $clientOptions = $this->getClientOptions(); |
||
231 | if (!empty($clientOptions)) { |
||
232 | $this->form->attributes[] = $clientOptions; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | 15 | $inputID = $this->getInputId(); |
|
237 | 15 | $attribute = Html::getAttributeName($this->attribute); |
|
238 | 15 | $options = $this->options; |
|
239 | 15 | $class = isset($options['class']) ? (array) $options['class'] : []; |
|
240 | 15 | $class[] = "field-$inputID"; |
|
241 | 15 | if ($this->model->isAttributeRequired($attribute)) { |
|
242 | 3 | $class[] = $this->form->requiredCssClass; |
|
243 | } |
||
244 | 15 | if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_CONTAINER && $this->model->hasErrors($attribute)) { |
|
245 | 3 | $class[] = $this->form->errorCssClass; |
|
246 | } |
||
247 | 15 | $options['class'] = implode(' ', $class); |
|
248 | 15 | $tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
249 | |||
250 | 15 | return Html::beginTag($tag, $options); |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Renders the closing tag of the field container. |
||
255 | * @return string the rendering result. |
||
256 | */ |
||
257 | 12 | public function end() |
|
258 | { |
||
259 | 12 | return Html::endTag(ArrayHelper::keyExists('tag', $this->options) ? $this->options['tag'] : 'div'); |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * Generates a label tag for [[attribute]]. |
||
264 | * @param null|string|false $label the label to use. If `null`, the label will be generated via [[Model::getAttributeLabel()]]. |
||
265 | * If `false`, the generated field will not contain the label part. |
||
266 | * Note that this will NOT be [[Html::encode()|encoded]]. |
||
267 | * @param null|array $options the tag options in terms of name-value pairs. It will be merged with [[labelOptions]]. |
||
268 | * The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded |
||
269 | * using [[Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered. |
||
270 | * @return $this the field object itself. |
||
271 | */ |
||
272 | 13 | public function label($label = null, $options = []) |
|
273 | { |
||
274 | 13 | if ($label === false) { |
|
275 | 4 | $this->parts['{label}'] = ''; |
|
276 | 4 | return $this; |
|
277 | } |
||
278 | |||
279 | 11 | $options = array_merge($this->labelOptions, $options); |
|
280 | 11 | if ($label !== null) { |
|
281 | 2 | $options['label'] = $label; |
|
282 | } |
||
283 | |||
284 | 11 | if ($this->_skipLabelFor) { |
|
285 | $options['for'] = null; |
||
286 | } |
||
287 | |||
288 | 11 | $this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $options); |
|
289 | |||
290 | 11 | return $this; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Generates a tag that contains the first validation error of [[attribute]]. |
||
295 | * Note that even if there is no validation error, this method will still return an empty error tag. |
||
296 | * @param array|false $options the tag options in terms of name-value pairs. It will be merged with [[errorOptions]]. |
||
297 | * The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded |
||
298 | * using [[Html::encode()]]. If this parameter is `false`, no error tag will be rendered. |
||
299 | * |
||
300 | * The following options are specially handled: |
||
301 | * |
||
302 | * - `tag`: this specifies the tag name. If not set, `div` will be used. |
||
303 | * See also [[\yii\helpers\Html::tag()]]. |
||
304 | * |
||
305 | * If you set a custom `id` for the error element, you may need to adjust the [[$selectors]] accordingly. |
||
306 | * @see $errorOptions |
||
307 | * @return $this the field object itself. |
||
308 | */ |
||
309 | 11 | public function error($options = []) |
|
310 | { |
||
311 | 11 | if ($options === false) { |
|
312 | 2 | $this->parts['{error}'] = ''; |
|
313 | 2 | return $this; |
|
314 | } |
||
315 | 9 | $options = array_merge($this->errorOptions, $options); |
|
316 | 9 | $this->parts['{error}'] = Html::error($this->model, $this->attribute, $options); |
|
317 | |||
318 | 9 | return $this; |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Renders the hint tag. |
||
323 | * @param string|bool $content the hint content. |
||
324 | * If `null`, the hint will be generated via [[Model::getAttributeHint()]]. |
||
325 | * If `false`, the generated field will not contain the hint part. |
||
326 | * Note that this will NOT be [[Html::encode()|encoded]]. |
||
327 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
328 | * the attributes of the hint tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
329 | * |
||
330 | * The following options are specially handled: |
||
331 | * |
||
332 | * - `tag`: this specifies the tag name. If not set, `div` will be used. |
||
333 | * See also [[\yii\helpers\Html::tag()]]. |
||
334 | * |
||
335 | * @return $this the field object itself. |
||
336 | */ |
||
337 | 14 | public function hint($content, $options = []) |
|
338 | { |
||
339 | 14 | if ($content === false) { |
|
340 | 3 | $this->parts['{hint}'] = ''; |
|
341 | 3 | return $this; |
|
342 | } |
||
343 | |||
344 | 11 | $options = array_merge($this->hintOptions, $options); |
|
345 | 11 | if ($content !== null) { |
|
346 | 1 | $options['hint'] = $content; |
|
347 | } |
||
348 | 11 | $this->parts['{hint}'] = Html::activeHint($this->model, $this->attribute, $options); |
|
349 | |||
350 | 11 | return $this; |
|
351 | } |
||
352 | |||
353 | /** |
||
354 | * Renders an input tag. |
||
355 | * @param string $type the input type (e.g. `text`, `password`) |
||
356 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
357 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
358 | * |
||
359 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
360 | * |
||
361 | * @return $this the field object itself. |
||
362 | */ |
||
363 | 2 | public function input($type, $options = []) |
|
364 | { |
||
365 | 2 | $options = array_merge($this->inputOptions, $options); |
|
366 | |||
367 | 2 | if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT && $this->model->hasErrors($this->attribute)) { |
|
368 | $options['class'][] = $this->form->errorCssClass; |
||
369 | } |
||
370 | |||
371 | 2 | $this->addAriaAttributes($options); |
|
372 | 2 | $this->adjustLabelFor($options); |
|
373 | 2 | $this->parts['{input}'] = Html::activeInput($type, $this->model, $this->attribute, $options); |
|
374 | |||
375 | 2 | return $this; |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * Renders a text input. |
||
380 | * This method will generate the `name` and `value` tag attributes automatically for the model attribute |
||
381 | * unless they are explicitly specified in `$options`. |
||
382 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
383 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
384 | * |
||
385 | * The following special options are recognized: |
||
386 | * |
||
387 | * - `maxlength`: int|bool, when `maxlength` is set `true` and the model attribute is validated |
||
388 | * by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. |
||
389 | * This is available since version 2.0.3. |
||
390 | * |
||
391 | * Note that if you set a custom `id` for the input element, you may need to adjust the value of [[selectors]] accordingly. |
||
392 | * |
||
393 | * @return $this the field object itself. |
||
394 | */ |
||
395 | 9 | public function textInput($options = []) |
|
404 | |||
405 | /** |
||
406 | * Renders a hidden input. |
||
407 | * |
||
408 | * Note that this method is provided for completeness. In most cases because you do not need |
||
409 | * to validate a hidden input, you should not need to use this method. Instead, you should |
||
410 | * use [[\yii\helpers\Html::activeHiddenInput()]]. |
||
411 | * |
||
412 | * This method will generate the `name` and `value` tag attributes automatically for the model attribute |
||
413 | * unless they are explicitly specified in `$options`. |
||
414 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
415 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
416 | * |
||
417 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
418 | * |
||
419 | * @return $this the field object itself. |
||
420 | */ |
||
421 | 3 | public function hiddenInput($options = []) |
|
429 | |||
430 | /** |
||
431 | * Renders a password input. |
||
432 | * This method will generate the `name` and `value` tag attributes automatically for the model attribute |
||
433 | * unless they are explicitly specified in `$options`. |
||
434 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
435 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
436 | * |
||
437 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
438 | * |
||
439 | * @return $this the field object itself. |
||
440 | */ |
||
441 | public function passwordInput($options = []) |
||
450 | |||
451 | /** |
||
452 | * Renders a file input. |
||
453 | * This method will generate the `name` and `value` tag attributes automatically for the model attribute |
||
454 | * unless they are explicitly specified in `$options`. |
||
455 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
456 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
457 | * |
||
458 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
459 | * |
||
460 | * @return $this the field object itself. |
||
461 | */ |
||
462 | 1 | public function fileInput($options = []) |
|
463 | { |
||
464 | // https://github.com/yiisoft/yii2/pull/795 |
||
465 | 1 | if ($this->inputOptions !== ['class' => 'form-control']) { |
|
466 | $options = array_merge($this->inputOptions, $options); |
||
467 | } |
||
468 | // https://github.com/yiisoft/yii2/issues/8779 |
||
469 | 1 | if (!isset($this->form->options['enctype'])) { |
|
470 | 1 | $this->form->options['enctype'] = 'multipart/form-data'; |
|
471 | } |
||
472 | 1 | $this->addAriaAttributes($options); |
|
473 | 1 | $this->adjustLabelFor($options); |
|
474 | 1 | $this->parts['{input}'] = Html::activeFileInput($this->model, $this->attribute, $options); |
|
475 | |||
476 | 1 | return $this; |
|
477 | } |
||
478 | |||
479 | /** |
||
480 | * Renders a text area. |
||
481 | * The model attribute value will be used as the content in the textarea. |
||
482 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
483 | * the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. |
||
484 | * |
||
485 | * If you set a custom `id` for the textarea element, you may need to adjust the [[$selectors]] accordingly. |
||
486 | * |
||
487 | * @return $this the field object itself. |
||
488 | */ |
||
489 | public function textarea($options = []) |
||
498 | |||
499 | /** |
||
500 | * Renders a radio button. |
||
501 | * This method will generate the `checked` tag attribute according to the model attribute value. |
||
502 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
503 | * |
||
504 | * - `uncheck`: string, the value associated with the uncheck state of the radio button. If not set, |
||
505 | * it will take the default value `0`. This method will render a hidden input so that if the radio button |
||
506 | * is not checked and is submitted, the value of this attribute will still be submitted to the server |
||
507 | * via the hidden input. If you do not want any hidden input, you should explicitly set this option as `null`. |
||
508 | * - `label`: string, a label displayed next to the radio button. It will NOT be HTML-encoded. Therefore you can pass |
||
509 | * in HTML code such as an image tag. If this is coming from end users, you should [[Html::encode()|encode]] it to prevent XSS attacks. |
||
510 | * When this option is specified, the radio button will be enclosed by a label tag. If you do not want any label, you should |
||
511 | * explicitly set this option as `null`. |
||
512 | * - `labelOptions`: array, the HTML attributes for the label tag. This is only used when the `label` option is specified. |
||
513 | * |
||
514 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
515 | * be HTML-encoded using [[Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered. |
||
516 | * |
||
517 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
518 | * |
||
519 | * @param bool $enclosedByLabel whether to enclose the radio within the label. |
||
520 | * If `true`, the method will still use [[template]] to layout the radio button and the error message |
||
521 | * except that the radio is enclosed by the label tag. |
||
522 | * @return $this the field object itself. |
||
523 | */ |
||
524 | public function radio($options = [], $enclosedByLabel = true) |
||
525 | { |
||
526 | if ($enclosedByLabel) { |
||
527 | $this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options); |
||
528 | $this->parts['{label}'] = ''; |
||
529 | } else { |
||
530 | if (isset($options['label']) && !isset($this->parts['{label}'])) { |
||
531 | $this->parts['{label}'] = $options['label']; |
||
532 | if (!empty($options['labelOptions'])) { |
||
533 | $this->labelOptions = $options['labelOptions']; |
||
534 | } |
||
535 | } |
||
536 | unset($options['labelOptions']); |
||
537 | $options['label'] = null; |
||
538 | $this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options); |
||
539 | } |
||
540 | $this->addAriaAttributes($options); |
||
541 | $this->adjustLabelFor($options); |
||
542 | |||
543 | return $this; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Renders a checkbox. |
||
548 | * This method will generate the `checked` tag attribute according to the model attribute value. |
||
549 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
550 | * |
||
551 | * - `uncheck`: string, the value associated with the uncheck state of the radio button. If not set, |
||
552 | * it will take the default value `0`. This method will render a hidden input so that if the radio button |
||
553 | * is not checked and is submitted, the value of this attribute will still be submitted to the server |
||
554 | * via the hidden input. If you do not want any hidden input, you should explicitly set this option as `null`. |
||
555 | * - `label`: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass |
||
556 | * in HTML code such as an image tag. If this is coming from end users, you should [[Html::encode()|encode]] it to prevent XSS attacks. |
||
557 | * When this option is specified, the checkbox will be enclosed by a label tag. If you do not want any label, you should |
||
558 | * explicitly set this option as `null`. |
||
559 | * - `labelOptions`: array, the HTML attributes for the label tag. This is only used when the `label` option is specified. |
||
560 | * |
||
561 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
562 | * be HTML-encoded using [[Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered. |
||
563 | * |
||
564 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
565 | * |
||
566 | * @param bool $enclosedByLabel whether to enclose the checkbox within the label. |
||
567 | * If `true`, the method will still use [[template]] to layout the checkbox and the error message |
||
568 | * except that the checkbox is enclosed by the label tag. |
||
569 | * @return $this the field object itself. |
||
570 | */ |
||
571 | public function checkbox($options = [], $enclosedByLabel = true) |
||
572 | { |
||
573 | if ($enclosedByLabel) { |
||
574 | $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options); |
||
575 | $this->parts['{label}'] = ''; |
||
576 | } else { |
||
577 | if (isset($options['label']) && !isset($this->parts['{label}'])) { |
||
578 | $this->parts['{label}'] = $options['label']; |
||
579 | if (!empty($options['labelOptions'])) { |
||
580 | $this->labelOptions = $options['labelOptions']; |
||
581 | } |
||
582 | } |
||
583 | unset($options['labelOptions']); |
||
584 | $options['label'] = null; |
||
585 | $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options); |
||
586 | } |
||
587 | $this->addAriaAttributes($options); |
||
588 | $this->adjustLabelFor($options); |
||
589 | |||
590 | return $this; |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Renders a drop-down list. |
||
595 | * The selection of the drop-down list is taken from the value of the model attribute. |
||
596 | * @param array $items the option data items. The array keys are option values, and the array values |
||
597 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
598 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
599 | * If you have a list of data models, you may convert them into the format described above using |
||
600 | * [[ArrayHelper::map()]]. |
||
601 | * |
||
602 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
603 | * the labels will also be HTML-encoded. |
||
604 | * @param array $options the tag options in terms of name-value pairs. |
||
605 | * |
||
606 | * For the list of available options please refer to the `$options` parameter of [[\yii\helpers\Html::activeDropDownList()]]. |
||
607 | * |
||
608 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
609 | * |
||
610 | * @return $this the field object itself. |
||
611 | */ |
||
612 | public function dropDownList($items, $options = []) |
||
621 | |||
622 | /** |
||
623 | * Renders a list box. |
||
624 | * The selection of the list box is taken from the value of the model attribute. |
||
625 | * @param array $items the option data items. The array keys are option values, and the array values |
||
626 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
627 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
628 | * If you have a list of data models, you may convert them into the format described above using |
||
629 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
630 | * |
||
631 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
632 | * the labels will also be HTML-encoded. |
||
633 | * @param array $options the tag options in terms of name-value pairs. |
||
634 | * |
||
635 | * For the list of available options please refer to the `$options` parameter of [[\yii\helpers\Html::activeListBox()]]. |
||
636 | * |
||
637 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
638 | * |
||
639 | * @return $this the field object itself. |
||
640 | */ |
||
641 | 2 | public function listBox($items, $options = []) |
|
650 | |||
651 | /** |
||
652 | * Renders a list of checkboxes. |
||
653 | * A checkbox list allows multiple selection, like [[listBox()]]. |
||
654 | * As a result, the corresponding submitted value is an array. |
||
655 | * The selection of the checkbox list is taken from the value of the model attribute. |
||
656 | * @param array $items the data item used to generate the checkboxes. |
||
657 | * The array values are the labels, while the array keys are the corresponding checkbox values. |
||
658 | * @param array $options options (name => config) for the checkbox list. |
||
659 | * For the list of available options please refer to the `$options` parameter of [[\yii\helpers\Html::activeCheckboxList()]]. |
||
660 | * @return $this the field object itself. |
||
661 | */ |
||
662 | public function checkboxList($items, $options = []) |
||
671 | |||
672 | /** |
||
673 | * Renders a list of radio buttons. |
||
674 | * A radio button list is like a checkbox list, except that it only allows single selection. |
||
675 | * The selection of the radio buttons is taken from the value of the model attribute. |
||
676 | * @param array $items the data item used to generate the radio buttons. |
||
677 | * The array values are the labels, while the array keys are the corresponding radio values. |
||
678 | * @param array $options options (name => config) for the radio button list. |
||
679 | * For the list of available options please refer to the `$options` parameter of [[\yii\helpers\Html::activeRadioList()]]. |
||
680 | * @return $this the field object itself. |
||
681 | */ |
||
682 | public function radioList($items, $options = []) |
||
691 | |||
692 | /** |
||
693 | * Renders a widget as the input of the field. |
||
694 | * |
||
695 | * Note that the widget must have both `model` and `attribute` properties. They will |
||
696 | * be initialized with [[model]] and [[attribute]] of this field, respectively. |
||
697 | * |
||
698 | * If you want to use a widget that does not have `model` and `attribute` properties, |
||
699 | * please use [[render()]] instead. |
||
700 | * |
||
701 | * For example to use the [[MaskedInput]] widget to get some date input, you can use |
||
702 | * the following code, assuming that `$form` is your [[ActiveForm]] instance: |
||
703 | * |
||
704 | * ```php |
||
705 | * $form->field($model, 'date')->widget(\yii\widgets\MaskedInput::className(), [ |
||
706 | * 'mask' => '99/99/9999', |
||
707 | * ]); |
||
708 | * ``` |
||
709 | * |
||
710 | * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly. |
||
711 | * |
||
712 | * @param string $class the widget class name. |
||
713 | * @param array $config name-value pairs that will be used to initialize the widget. |
||
714 | * @return $this the field object itself. |
||
715 | */ |
||
716 | 1 | public function widget($class, $config = []) |
|
734 | |||
735 | /** |
||
736 | * Adjusts the `for` attribute for the label based on the input options. |
||
737 | * @param array $options the input options. |
||
738 | */ |
||
739 | 18 | protected function adjustLabelFor($options) |
|
749 | |||
750 | /** |
||
751 | * Returns the JS options for the field. |
||
752 | * @return array the JS options. |
||
753 | */ |
||
754 | 5 | protected function getClientOptions() |
|
825 | |||
826 | /** |
||
827 | * Checks if client validation enabled for the field. |
||
828 | * @return bool |
||
829 | * @since 2.0.11 |
||
830 | */ |
||
831 | 4 | protected function isClientValidationEnabled() |
|
835 | |||
836 | /** |
||
837 | * Checks if ajax validation enabled for the field. |
||
838 | * @return bool |
||
839 | * @since 2.0.11 |
||
840 | */ |
||
841 | 4 | protected function isAjaxValidationEnabled() |
|
845 | |||
846 | /** |
||
847 | * Returns the HTML `id` of the input element of this form field. |
||
848 | * @return string the input id. |
||
849 | * @since 2.0.7 |
||
850 | */ |
||
851 | 18 | protected function getInputId() |
|
855 | |||
856 | /** |
||
857 | * Adds aria attributes to the input options. |
||
858 | * @param $options array input options |
||
859 | * @since 2.0.11 |
||
860 | */ |
||
861 | 15 | protected function addAriaAttributes(&$options) |
|
874 | } |
||
875 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: