mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-16 22:39:52 +08:00
Merge pull request #9294 from yiisoft/activefield-id
alternative solution for issue #7627
This commit is contained in:
@@ -143,6 +143,12 @@ class ActiveField extends Component
|
||||
*/
|
||||
public $parts = [];
|
||||
|
||||
/**
|
||||
* @var string this property holds a custom input id if it was set using [[inputOptions]] or in one of the
|
||||
* `$options` parameters of the `input*` methods.
|
||||
*/
|
||||
private $_inputId;
|
||||
|
||||
|
||||
/**
|
||||
* PHP magic method that returns the string representation of this object.
|
||||
@@ -169,11 +175,11 @@ class ActiveField extends Component
|
||||
* and use them as the content.
|
||||
* If a callable, it will be called to generate the content. The signature of the callable should be:
|
||||
*
|
||||
* ~~~
|
||||
* ```php
|
||||
* function ($field) {
|
||||
* return $html;
|
||||
* }
|
||||
* ~~~
|
||||
* ```
|
||||
*
|
||||
* @return string the rendering result
|
||||
*/
|
||||
@@ -213,7 +219,7 @@ class ActiveField extends Component
|
||||
}
|
||||
}
|
||||
|
||||
$inputID = Html::getInputId($this->model, $this->attribute);
|
||||
$inputID = $this->getInputId();
|
||||
$attribute = Html::getAttributeName($this->attribute);
|
||||
$options = $this->options;
|
||||
$class = isset($options['class']) ? [$options['class']] : [];
|
||||
@@ -673,7 +679,11 @@ class ActiveField extends Component
|
||||
*/
|
||||
protected function adjustLabelFor($options)
|
||||
{
|
||||
if (isset($options['id']) && !isset($this->labelOptions['for'])) {
|
||||
if (!isset($options['id'])) {
|
||||
return;
|
||||
}
|
||||
$this->_inputId = $options['id'];
|
||||
if (!isset($this->labelOptions['for'])) {
|
||||
$this->labelOptions['for'] = $options['id'];
|
||||
}
|
||||
}
|
||||
@@ -712,7 +722,7 @@ class ActiveField extends Component
|
||||
|
||||
$options = [];
|
||||
|
||||
$inputID = Html::getInputId($this->model, $this->attribute);
|
||||
$inputID = $this->getInputId();
|
||||
$options['id'] = $inputID;
|
||||
$options['name'] = $this->attribute;
|
||||
|
||||
@@ -748,4 +758,14 @@ class ActiveField extends Component
|
||||
'error' => '.help-block',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTML `id` of the input element of this form field.
|
||||
* @return string the input id.
|
||||
* @since 2.0.6
|
||||
*/
|
||||
protected function getInputId()
|
||||
{
|
||||
return $this->_inputId ?: Html::getInputId($this->model, $this->attribute);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user