yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

CHtml::activeLabel and tabular input #2508

Closed pavimus closed 11 years ago

pavimus commented 11 years ago

currently activeLabel generates wrong label for tabular input if 'for' html option specified. It generates something '[0]value' instead 'Value'. To fix this problem, CHtml::activeLabel must be changed from

    public static function activeLabel($model,$attribute,$htmlOptions=array())
    {
        if(isset($htmlOptions['for']))
        {
            $for=$htmlOptions['for'];
            unset($htmlOptions['for']);
        }
        else  {
            $for=self::getIdByName(self::resolveName($model,$attribute));
        }
        if(isset($htmlOptions['label']))
        {
            if(($label=$htmlOptions['label'])===false)
                return '';
            unset($htmlOptions['label']);
        }
        else {
            $label=$model->getAttributeLabel($attribute);
        }
        if($model->hasErrors($attribute))
            self::addErrorCss($htmlOptions);

        return self::label($label,$for,$htmlOptions);
    }

to

    public static function activeLabel($model,$attribute,$htmlOptions=array())
    {
        $name=self::resolveName($model,$attribute);
        if(isset($htmlOptions['for']))
        {
            $for=$htmlOptions['for'];
            unset($htmlOptions['for']);
        }
        else  {
            $for=self::getIdByName($name);
        }
        if(isset($htmlOptions['label']))
        {
            if(($label=$htmlOptions['label'])===false)
                return '';
            unset($htmlOptions['label']);
        }
        else {
            $label=$model->getAttributeLabel($attribute);
        }
        if($model->hasErrors($attribute))
            self::addErrorCss($htmlOptions);

        return self::label($label,$for,$htmlOptions);
    }
klimov-paul commented 11 years ago

Unable to reproduce.

Also the provided code fix actually changes nothing.

pavimus commented 11 years ago

Please take look at CHtml::resolveName function. it can modify $attribute argument, that is passed by reference. In tabular input $attribute is something like '[0]value'. After calling $name=self::resolveName($model,$attribute); $attribute will be changed to 'value'. In non-fixed version if $htmlOptions has no 'for' key, the $attribute will be translated from '[0]value' to 'value', if $htmlOptions has 'for' key - $attribute will stay unmodified, because CHtml::resolveName will not be called.

klimov-paul commented 11 years ago

Issue reproduced.

klimov-paul commented 11 years ago

Solution confirmed

klimov-paul commented 11 years ago

Fix has been prepared. Switching to milestone 1.1.14