ZF-10450: Zend_Form: Prepending empty elements like <br /> with standards decorators

Description

I want prepend
to an form element. I use the following decorator:

protected $_elementDecorators = array( 'ViewHelper', 'Description', 'Errors', array(array('LineBreak' => 'HtmlTag'), array('tag' => 'br', 'placement' => 'prepend')), 'Label', array('HtmlTag', array('tag' => 'div', 'class' => 'zend_element')), );

The result of the LineBreak decoration is "
", but with options like "openOnly" it is invalid XHTML. It seems where is no way to place an empty tag like
with help of the standard decorators to the form.

Comments

Zend_Form_Decorator_HtmlTag does not appear to support these types of tags.

The decorator allows for openOnly and closeOnly but not for tags that require /> at the end. This would also be the case for


.

Since there are only a small number of tags that are affected by this (
,


, others?) then I propose that the Zend_Form_Decorator_HtmlTag is adapted to recognise these tags and render them as . Note: tags will not be handled by Zend_Form_Decorator_HtmlTag as they have their own classes.

The alternative would be to add another optional argument to Zend_Form_Decorator_HtmlTag, 'singleTag', which would render the tag with /> at the end.

I would like to work on this issue but I think we need some additional input before I start as to which of the above resolutions would be best.

G

@Andreas Please use always the code tags and show us the desired output.

Here is a workaround: (very simple with PHP 5.3)


protected $_elementDecorators = array(
    'ViewHelper',
    'Description',
    'Errors',
    array(
        // Decorator name
        'Callback',
        // Options
        array(
            'callback' => function($content, $element, array $options)
            {
                return '
'; }, 'placement' => 'prepend', ) ), 'Label', array( // Decorator name 'HtmlTag', // Options array( 'tag' => 'div', 'class' => 'zend_element', ), ), );

"Callback" is a standard decorator!

Have added a patch.

A new option is available, 'selfClosing'. This option requires a placement to be set as you cannot have content within a self closing tag. Throws a Zend_Form_Decorator_Exception if no placement set.

Guy

This is a bad idea:


protected function _getSelfClosingTag($tag, array $attribs = null)
{
    // ...
    $html .= ' />';
}

XHTML vs. HTML!

What would you suggest as a solution? Shouldnt the framework be XHTML compliant?

Both: HTML and XHTML!


$element = $this->getElement();
$view    = $element->getView();

// XHTML or HTML end tag?
$endTag = ' />';
if (($view instanceof Zend_View_Abstract) && !$view->doctype()->isXhtml()) {
    $endTag= '>';
}

Ok, will amend code and tests and resubmit the patch. G

I have updated the patch to differentiate between HTML and XHTML. I have also split the patch into two files (tests and library).

Please could someone review the patches.

Thanks

G

@Guy Halford-Thompson Is this patch operational yet? If so could you provide a usage example please?

I use:

->addDecorator('HtmlTag', array('tag' => 'br', 'placement' => 'prepend', 'selfClosing' => true))

but that gives me:
....

thanks, T

Hi Timmo, have you add the patch to your local library?