views:

71

answers:

1

I have a simple content edit form (Zend_Form) that I populate() with an array generated from the Zend_Db_Table_Row_Abstract->toArray() method. It works quite well.

The goal is to not populate one of the form elements with a value.

I'm currently solving this by removing that specific key => value pair from the populating array. I'm curious if there's a way to do that when generating the form element itself.

Say...

$foo = new Zend_Form_Element_Textarea('foo');
$foo->ignoreDefaultValue(true);
$form->addElement($foo);

Any thoughts?

A: 

Nope. When you do $form->validate($data) or $form->populate($data) - internally this is simple

foreach($formElements as $element) {
    $element->setDefault($newValue);
}

theare no special internal flags like $form->_populationInProgress = true

u can achive this easy by extending Zend_Form

class App_Form extends Zend_Form
{
    protected $_ignoreDefaults = array();

    public function addIgnoredDefaultsElement(Zend_Form_Element $el)
    {
        $this->_ignoreDefaults[$el->getName()] = true;
    }

    public function isInIgnoregDefaults($name)
    {
        return (bool)array_key_exists($name, $this->_ignoreDefaults);
    }

    public function setDefault($name, $value) 
    {
        if (!$this->isInIgnoregDefaults($name)) {
            parent::setDefault($name, $value);
        }
        return $this;    
    }
}
SM