0

I have the following element being created in Zend Form:

    // Add the submit button
    $this->addElement('button', 'cancel', array(...)
    ));

Which creates a button element as expected. What if I want to create a button element with span tag nested inside like so:

<button ...> 
<span>Cancel</span> 
</button> 

Any ideas?

3
  • Maybe this helps: stackoverflow.com/questions/778770/…. What you need is decorators. Commented Apr 4, 2011 at 18:00
  • This is an exact duplicate: stackoverflow.com/questions/778770/… Commented Apr 4, 2011 at 19:17
  • @markus: That what I thought too. But decorators won't help much with the button element unfortunately, because it's not a decorator itself. And if it was, it would have to wrap also. Commented Apr 4, 2011 at 22:27

1 Answer 1

2

The problem with the answers in the other question is that it renders the buttons translatable capability useless.

Here's my solution. It's practically a copy of both Zend_Form_Element_Button and Zend_View_Helper_FormButton, with added functionality.

The form element:

class App_Form_Element_ButtonPadded
    extends Zend_Form_Element_Button
{
    public $helper = 'formButtonPadded';

    public function init()
    {
        $this->getView()->addHelperPath( 'App/View/Helper', 'App_View_Helper' );
    }
}

The view helper:

class App_View_Helper_FormButtonPadded
    extends Zend_View_Helper_FormElement
{

    public function formButtonPadded( $name, $value = null, $attribs = null )
    {
        $info    = $this->_getInfo( $name, $value, $attribs );
        extract( $info ); // name, id, value, attribs, options, listsep, disable, escape

        // Get content
        $content = '';
        if( isset( $attribs[ 'content' ] ) )
        {
            $content = $attribs[ 'content' ];
            unset( $attribs[ 'content' ] );
        } else {
            $content = $value;
        }

        // Ensure type is sane
        $type = 'button';
        if( isset( $attribs[ 'type' ] ) )
        {
            $attribs[ 'type' ] = strtolower( $attribs[ 'type' ] );
            if( in_array( $attribs[ 'type' ], array( 'submit', 'reset', 'button' ) ) )
            {
                $type = $attribs[ 'type' ];
            }
            unset( $attribs[ 'type' ] );
        }

        // build the element
        if( $disable )
        {
            $attribs[ 'disabled' ] = 'disabled';
        }

        $content = ( $escape ) ? $this->view->escape( $content ) : $content;

        $xhtml = '<button'
                . ' name="' . $this->view->escape( $name ) . '"'
                . ' id="' . $this->view->escape( $id ) . '"'
                . ' type="' . $type . '"';

        // add a value if one is given
        if( !empty( $value ) )
        {
            $xhtml .= ' value="' . $this->view->escape( $value ) . '"';
        }

        $paddingTag = 'span';
        if( isset( $attribs[ 'paddingTag' ] ) )
        {
            $paddingTag = strtolower( (string) $attribs[ 'paddingTag' ] );
            $paddingTag = strlen( $paddingTag ) ? $paddingTag : 'span';
            unset( $attribs[ 'paddingTag' ] );
        }

        $paddingTagRepeat = 1;
        if( isset( $attribs[ 'paddingTagRepeat' ] ) && $attribs[ 'paddingTagRepeat' ] >= 0 )
        {
            $paddingTagRepeat = (int) $attribs[ 'paddingTagRepeat' ];
            unset( $attribs[ 'paddingTagRepeat' ] );
        }

        $paddingStartTag = '<' . $paddingTag . '>';
        $paddingEndTag = '</' . $paddingTag . '>';
        $content = str_repeat( $paddingStartTag, $paddingTagRepeat ) . 
                   $content .
                   str_repeat( $paddingEndTag, $paddingTagRepeat );

        // add attributes and close start tag
        $xhtml .= $this->_htmlAttribs( $attribs ) . '>';

        // add content and end tag
        $xhtml .= $content . '</button>';

        return $xhtml;
    }
}

Possible usage:

$paddedButton = new App_Form_Element_ButtonPadded( array(
    'type' => 'submit',
    'label' => 'Your possibly translatable label',
    'paddingTag' => 'span',
    'paddingTagRepeat' => 2 // I've had to use double span's inside the button for my app
) );

Improvements welcomed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.