Zend Framework 2 RC3 Zend \ Form # getData ()

It is interesting that I am doing something wrong or if it is an error in ZF2: when I try to set some data on the form, check it and extract the data, it is just an empty array.

I extracted this code from some classes to simplify the task

    $form = new \Zend\Form\Form;
    $form->setInputFilter(new \Zend\InputFilter\InputFilter);
    $form->add(array(
        'name' => 'username',
        'attributes' => array(
            'type'  => 'text',
            'label' => 'Username',
        ),
   ));

   $form->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Register',
        ),
    ));

    if ($this->getRequest()->isPost()) {

        $form->setData($this->getRequest()->getPost()->toArray());
        if ($form->isValid()) {

            echo '<pre>';
            print_r($form->getData());
            print_r($form->getMessages());
            echo '</pre>';
        }
    }

both print_r()show empty arrays. I do not receive any data from the form, as well as messages. Is this my mistake or ZF2?

+5
source share
1 answer

Thanks to @SamuelHerzog and @Sam, the form requires input filters for all elements. In the case of the form described in the question, this short code is enough to make it work at all.

    $inputFilter = new InputFilter();
    $factory     = new InputFactory();

    $inputFilter->add($factory->createInput(array(
        'name'     => 'username'
    )));

    $form->setInputFilter($inputFilter);

, inpoutFilter . .

+7

All Articles