From 13fca3bc54e22e9e89f9b86eeedbfb87a248d750 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 3 Aug 2015 12:29:43 +0200 Subject: [PATCH] alternative solution for issue #7627 should cover more cases then #8912. Have not tested it yet. see #7627 --- framework/widgets/ActiveField.php | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/framework/widgets/ActiveField.php b/framework/widgets/ActiveField.php index be9bf7b70d..f13d311e20 100644 --- a/framework/widgets/ActiveField.php +++ b/framework/widgets/ActiveField.php @@ -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); + } }