views:

38

answers:

1

So I've created myself a custom form element which has a custom view helper. Now I want to be able to set certain parameters/variables on this form element and be able to access them in my element's view helper. How can I do that?

Here's an example of what I am talking about:

adding the element to the form:

$element = new My_Form_Element_Picker('elementname');
$element->setFoobar('hello');
// or 
$form->addElement('Picker', 'elementname', array('foobar' => 'hello'));

form element:

class My_Form_Element_Picker extends Zend_Form_Element_Xhtml
{
    public $helper = 'pickerElement';
}

view helper:

class My_View_Helper_PickerElement extends Zend_View_Helper_FormElement
{
    public function pickerElement($name, $value = null, $attribs = null)
    {
        //now I want to check if the 'foobar' option was set, otherwise use a default value
        $foobar = 'default';
    }
}
A: 

There is a fourth optional argument to the view helper that might do the trick for you.

if you define your view helper like this:

public function pickerElement( $name, $value=null, $attribs=null, $options=null ) { }

And then inside your actual form element you define it like this:

class My_Form_Element_Picker extends Zend_Form_Element_Xhtml {

 public $helper = 'pickerElement';
 public $options = array();

 public function setFoobar( $foobar ) {
  $this->options['foobar'] = $foobar;
 }
}

You will find that the options are passed into the view helper and can be used.

This code is from memory so please forgive any mistakes, this method definitely works for me though.

Matt Wheeler
your memory is correct. =]
Andrew