Issues

ZF-2913: Legend tags for fieldset is added even when legend is empty

Description

Problem: When using subforms and Zend_Layout, legend tags for fieldset is added even when legend is empty, which (at least in Firefox 2.0.0.12) may result in an empty space in the graphical output for the fieldset border where the legend would be.

Various setLegend methods (in Zend_Form, Zend_Form_DisplayGroupand Zend_Form_Decorator_Fieldset) will force string type, so after Zend_Form_Decorator_Fieldset::getLegend() calls $this->setLegend($legend) on line 92 and $legend is NULL, $this->_legend will be an empty string instead of the correct NULL value.

This may also trigger a strange behaviour/bug (ZF-2914) with Zend_Translate if using the gettext adapter - when trying to translate the empty string in method Zend_Form_Decorator_Fieldset::render(), the gettext file header information will be displayed.

Reproduction code: index.php


<?php
require_once 'Zend/Loader.php';
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Form.php';
require_once 'Zend/Form/SubForm.php';
require_once 'Zend/Layout.php';
Zend_Layout::startMvc();
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory('./app/controllers');
$frontController->dispatch();
app/controllers/indexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    function indexAction()
    {
        $this->view->form = new Zend_Form;
        $this->view->form->addSubForm(new Zend_Form_SubForm(array(
            'action' => Zend_Controller_Front::getInstance()->getBaseUrl() . '/form/test',
            'method' => 'post',
            'elements' => array(
                'test' => 'text',
                'submit' => 'submit'
            )
        )), 'name');
    }
}
app/views/scripts/layout.phtml

<?php echo $this->render('index/index.phtml'); ?>

app/views/scripts/index/index.phtml


<?php echo $this->form; ?>

Suggested solution: Don't call setLegend when new value should be NULL.

zend/form/decorator/fieldset.php

line 90-93:


            if (method_exists($element, 'getLegend')) {
                $legend = $element->getLegend();
                $this->setLegend($legend);
            }

replace with:


            if (method_exists($element, 'getLegend')) {
                $legend = $element->getLegend();
                if ($legend !== null)
                {
                    $this->setLegend($legend);
                }
            }

Comments

Please categorize/fix as needed.

Scheduling for next mini release.

Resolved in trunk and 1.5 release branch