Matthew Weier O'Phinney-3 wrote:
> 
> -- petewilliams1983 <[email protected]> wrote
> (on Monday, 22 December 2008, 05:02 AM -0800):
>> I'm trying to do exactly the same thing. So far the best solution I can
>> come
>> up with is to pass my custom options in via either the $attribs or
>> $options
>> arrays. You can then process those options within your view helper, and
>> delete them from the array so that they aren't added to the element
>> markup.
>> 
>> However I'm pretty sure this isn't the best way of doing things.
> 
> Actually, this is exactly how it should work. :)
> 
> If you look at the current decorator implementations, they pass the
> attributes set in the element directly to the view helper as the
> $attribs array.
> 
> -- 
> Matthew Weier O'Phinney
> Software Architect       | [email protected]
> Zend Framework           | http://framework.zend.com/
> 
> 

Thanks Matthew, that was a lucky guess on my part! I'm still struggling a
little bit with the exact implementation and was hoping you could take a
quick look at an element I'm working on at the moment. Basically I've
created my own Submit element which will also place a 'Cancel' link next to
the button. I want to be able to specify the link URL and text (defaults to
'Cancel').

I instantiate an element like so:

$submit = new My_Form_Element_Submit('submit',
    array(
        'label' => 'Save',
        'buttonParams' => array(
            'linkText' => 'Cancel Changes',
            'linkUrl' => array(
                'module' => 'admin',
                'controller' => 'pages',
                'action' => 'view'
            )
        )
    )
);

--------------------------------------------------

ELEMENT:

class My_Form_Element_Submit extends Zend_Form_Element_Submit
{
   public $helper = 'formSubmit';
}

--------------------------------------------------

VIEW HELPER:

class My_View_Helper_FormSubmit extends Zend_View_Helper_FormSubmit
{
    public function formSubmit($name, $value=null, $attribs=null)
    {
        if(isset($attribs['buttonParams'])) {
            $params = $attribs['buttonParams'];
            unset($attribs['buttonParams']);
        }

        $button = parent::formSubmit($name, $value, $attribs);

        if(isset($params['linkUrl'])) {
            // Build the link URL
            $url = $this->view->url($params['linkUrl']);

            // Set the link text
            $linkText = isset($params['linkText']) ? $params['linkText'] :
'Cancel';

            // Create the link
            $button .= ' or  ' . $url . ' ' . $linkText . ' ';
        }

        return $button;
    }
}

--------------------------------------------------

I've had a look at the current elements/decorators/view helpers, including
the Dojo ones, but I'm struggling to get my head around them. Any advice
would be greatly appreciated.

Thanks,

Pete
-- 
View this message in context: 
http://www.nabble.com/Custom-form-element---passing-options-to-viewHelper-tp21073843p21144983.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to