8

I have a zend form in which I have many elements. I use form on many places. I have an element of file and that is:

$file= new Zend_Form_Element_File('merchantLogo');
        $file->setDestination("application_data/merchant/logo/original/");
        $file->addValidator('Count', false, 1);
        $file->addValidator('Extension', false, 'jpg,png,gif,jpeg');
        $file->setDecorators(array('ViewHelper','Errors'));

Now What I want to ask that how can I unset any element of this zend form. I want this because although on my one action I am not using this element, but this element is creating problem. So how to unset any element?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137

4 Answers4

12

Use Zend_Form::removeElement(). See http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.elements.methods

Example

$form->removeElement('merchantLogo');
Phil
  • 157,677
  • 23
  • 242
  • 245
4

This is quiete easy:

$this->removeElement('merchantLogo');

The public function removeElement() on the Zend_Form takes the name of an element and removes it from the form.

See: http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.elements.methods

Hikaru-Shindo
  • 1,891
  • 12
  • 22
3

1:

$element = new Zend_Form_Element_Text('test');
if ($condition) {
    $form->addElement($element);
}

2:

$element = new Zend_Form_Element_Text('test');
$form->addElement($element);
if (!$condition) {
    $form->removeElement('test');
}
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
0

$form = $form->remove('merchantLogo'); // or chain to remove ->('anotherElement');

Newbie note:

Replace $form with the return value of remove(') method to use the updated form.

KevDev
  • 59
  • 4