Question

I have problem:

When I fill in form and pressing add button page is reloaded, but no data is added to the database.

Code of NewsController, add action is below:

public function addAction() {
    $form = new AddNewsForm();
    $form->get('submit')->setValue('Add1');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());
        var_dump($form->isValid());
        if ($form->isValid()) {
            echo "form is valid";
            $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

            $blogpost = new NewsItem();

            $blogpost->exchangeArray($form->getData());

            $blogpost->setCreated(time());
            $blogpost->setUserId(0);

            $objectManager->persist($blogpost);
            $objectManager->flush();

            // Redirect to list of blogposts
            return $this->redirect()->toRoute('news');
        }
    }
    return array('form' => $form);
}

Class AddNewsForm is included as use \News\Form\AddNewsForm as AddNewsForm; above.

I tried to debug my code and realized, that $form->isValid() return false all time. I tried to fill in all fields of form — it says that form is not valid. If not all fields are filled in it false too.

The problem is with validation, I think, so I will add here how I assing filter to the form. This is how I assing filter to my form:

$this->setInputFilter(new AddNewsInputFilter());

Class AddNewsInputFilter is included by this:

use \News\Form\AddNewsInputFilter as AddNewsInputFilter;

I don't think it is good to paste there ~100 lines of code, so I will just give a link to files in my github repo (full code of controllers/files available here):

So the problem is that $form->isValid(); doesn't show is form valid or not properly and I don't know why. Note, that request is getting properly and first condition is passed (but second is not passed). It is the problem, thats why I am writing here.

How I can solve it?

Thanks is advance!

Was it helpful?

Solution

try var_dump($form->getMessages()) and var_dump($form->getInputFilter()->getMessages()) in controller(after calling $form->isValid()) or in view . see what error you getting and on witch element ?

NOTICE : getMessages() will be empty if $form->isValid() has not been called yet,

UPDATE : do this in controller :

var_dump($form->isValid());
var_dump($form->getMessages())
var_dump($form->getInputFilter()->getMessages())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top