Zend Form: Call to a member function setValue() on a non-object

1.9k views Asked by At

i have this error:

Call to a member function setValue() on a non-object in

when i try to populate a form with my storeged data. The think most strange, is that if i use the same method on other forms, it works fine.

My code is:

on zend form abstract:

class App_Form_BootstrapForm extends Zend_Form
{

    public function __construct($options = null)
    {
        // decorator style
    }

    public function populate($data)
    {
        foreach ($data as $field => $value) {
            $this->{$field}->setValue($value);
        }
        return $this;
    }
}

here my form:

class Application_Form_Admin_Pneumatico_Pneumatico extends App_Form_BootstrapForm
{

    public function init()
    {
        $this->setAttrib("vertical", true);

        $this->setMethod('post');
        $this->setName('pneumatico');
        $this->setAction('');
        $this->setAttrib('enctype', 'multipart/form-data');

        $this->addElement('text', 'codice', array(
            'filters' => array('StringTrim'),
            'validators' => array(
                array('StringLength', true, array(3, 50))
            ),
            'required' => true,
            'placeholder' => 'Codice prodotto',
            'class' => 'form-control',
            'label' => 'Codice (*)'
        ));
        // other form elements
    }
}

I tryed to pass the form and the value to insert to the view and then use populate, but i had the same error. I tryed to do that in controller before send it to the view, but i get the same error.

Someone can help me?

1

There are 1 answers

1
Adrian World On BEST ANSWER

Your error should be coming from the populate function here

$this->{$field}->setValue($value);

and if one of the keys in that $data array is not a form value $this->{$field} will cause non-object error.

Check all the keys in the data array first.