Issues

ZF-6667: New "IMPLICIT" placement for Zend_Form_Decorator_Label

Issue Type: Improvement Created: 2009-05-14T21:18:49.000+0000 Last Updated: 2012-05-30T13:51:09.000+0000 Status: Resolved Fix version(s): - 1.12.0 (27/Aug/12)

Reporter: Alexandre Gomes Gaigalas (alganet) Assignee: Kai Uwe (kaiuwe) Tags: - Zend_Form

  • FixForZF1.12
  • zf-crteam-priority
  • zf-crteam-review

Related issues: - ZF-9722

Attachments: - Label.php.patch

Description

Zend_Form_Decorator_Label has two placement modes: PREPEND and APPEND, described below:

PREPEND:

<pre class="highlight">
Name 

APPEND:

<pre class="highlight">
Name

IMPLICIT (currently not implemented):

<pre class="highlight">
Name 

Sample form using this technique: http://gaigalas.net/semanticforms.html

HTML reference for "implicit labels": http://w3.org/TR/html401/…

Comments

Posted by Kai Uwe (kaiuwe) on 2010-12-19T12:37:29.000+0000

There is a problem:

<pre class="highlight">
Name 
 Name

Prepend or append? ;-)

My suggestion:

<pre class="highlight">
const IMPLICIT = 'IMPLICIT';
const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';



<pre class="highlight">
Name 

And

<pre class="highlight">
const IMPLICIT_APPEND = 'IMPLICIT_APPEND';



<pre class="highlight">
 Name

I think the priority "major" is greatly exaggerated!

Posted by Kai Uwe (kaiuwe) on 2010-12-30T04:50:28.000+0000

<pre class="highlight">
$form = new Zend_Form();

$form->addElement(
    'text',
    'name',
    array(
        'label'       => 'Name',
        'description' => 'Workaround for implicit labels.',
        'decorators'  => array(
            'ViewHelper',
            array(
                // Decorator name
                'Callback',
                // Options
                array(
                    'callback' => function($content, $element, array $options)
                    {
                        // Get label
                        $label = $element->getLabel();

                        // Remove label from the element
                        $element->setLabel(null);

                        return $label;
                    },
                    'placement' => 'prepend',
                )
            ),
            array(
                // Decorator name
                array(
                    'label' => 'HtmlTag',
                ),
                // Options
                array(
                    'tag' => 'label',
                ),
            ),
            'Errors',
            'Description',
            array(
                // Decorator name
                array(
                    'dd' => 'HtmlTag',
                ),
                // Options
                array(
                    'tag' => 'dd',
                ),
            ),
            array(
                // Decorator name
                'Label',
                // Options
                array(
                    'tag' => 'dt',
                ),
            ),
        ),
    )
);

echo $form->render(new Zend_View());



<pre class="highlight">



 
        
            Name
                

Workaround for implicit labels.

Posted by Kai Uwe (kaiuwe) on 2010-12-30T04:53:46.000+0000

Priority changed

Posted by Kai Uwe (kaiuwe) on 2010-12-30T05:53:52.000+0000

Proposal:

<pre class="highlight">
class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
{
    // ...
    
    const IMPLICIT         = 'IMPLICIT';
    const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';
    const IMPLICIT_APPEND  = 'IMPLICIT_APPEND';
    
    // ...
    
    public function getPlacement()
    {
        $placement = $this->_placement;
        if (null !== ($placementOpt = $this->getOption('placement'))) {
            $placementOpt = strtoupper($placementOpt);
            switch ($placementOpt) {
                case self::APPEND:
                case self::PREPEND:
                case self::IMPLICIT:
                case self::IMPLICIT_PREPEND:
                case self::IMPLICIT_APPEND:
                    $placement = $this->_placement = $placementOpt;
                    break;
                case false:
                    $placement = $this->_placement = null;
                    break;
                default:
                    break;
            }
            $this->removeOption('placement');
        }

        return $placement;
    }
    
    // ...
    
    public function render($content)
    {
        // ...
        
        if (!empty($label)) {
            $options['class'] = $class;
            
            $label = trim($label);
            
            switch ($placement) {
                case self::IMPLICIT:
                    // Break was intentionally omitted
                
                case self::IMPLICIT_PREPEND:
                    $options['escape']     = false;
                    $options['disableFor'] = true;
                    
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $label . $separator . $content,
                        $options
                    );
                    break;
                    
                case self::IMPLICIT_APPEND:
                    $options['escape']     = false;
                    $options['disableFor'] = true;
                    
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $content . $separator . $label,
                        $options
                    );
                    break;
                    
                case self::APPEND:
                    // Break was intentionally omitted
                
                case self::PREPEND:
                    // Break was intentionally omitted
                
                default:
                    $label = $view->formLabel(
                        $element->getFullyQualifiedName(),
                        $label,
                        $options
                    );
                    break;
            }
        } else {
            $label = ' ';
        }
        
        // ...
        
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
                
            case self::PREPEND:
                return $label . $separator . $content;
                
            case self::IMPLICIT:
                // Break was intentionally omitted
                
            case self::IMPLICIT_PREPEND:
                // Break was intentionally omitted
                
            case self::IMPLICIT_APPEND:
                return $label;
        }
    }
}



<pre class="highlight">
$form = new Zend_Form();

$form->addElement(
    'text',
    'name',
    array(
        'label' => 'Name',
        'decorators' => array(
            'ViewHelper',
            'Errors',
            'Description',
            array(
                'Label',
                array(
                    'placement' => Zend_Form_Decorator_Label::IMPLICIT_APPEND,
                ),
            ),
            'DtDdWrapper',
        ),
    )
);

echo $form->render(new Zend_View());



<pre class="highlight">



 
        
            
                
                Name
            
        
    

Posted by Frank Brückner (frosch) on 2012-03-12T21:31:56.000+0000

Patch and unit tests added.

Posted by Adam Lundrigan (adamlundrigan) on 2012-05-30T13:51:09.000+0000

Fixed in trunk (1.12.): r24834

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts