1
votes

I'm using a form which contains wrapped elements. The wrapping happens in the view like described here.

My action looks like this:

 $myForm = [definition here]
 $myForm->setName('entity');
 $myForm->setWrapElements(true);

 $request = $this->getRequest();
 if ($request->isPost()) {

        $myEntity = new Entity();
        $myForm->bind($myEntity);
        $myForm->setData($request->getPost()->get('entity'));

The problem: When calling $myForm->isValid() it's invalid. When calling $myForm->getData() afterwards it's empty.

I repeated the setName and setWrapElements in the action but with or without it, it doesn't work.

Any ideas what I might be doing wrong? The form definition is untouched and works for non-wrapped forms. So I guess the error is not in there.

P.S.: An echo of $myForm->isValid() returns an empty string. Is there maybe a way to get the error message? The form fields are filled with the data I've put in there and don't show any errors.

3
You can use $form->getMessages(); to get the validation messages - Aydin Hassan
var_dump on this plz -> $request->getPost()->get('entity') - Sina Miandashti
@AydinHassan: If you create an answer from your comment I'd accept it. it solved my problem after all :) - Ron

3 Answers

1
votes

Using the following:

$form->getMessages()

Will give you the validation messages.

You can dump the contents or loop the messages in a foreach loop. For example:

foreach($form->getMessages() as $msgId => $msg) {
    echo "Validation error: $msgId => $msg"
}
0
votes

can you try to add line to your code, as I can see in zend's Form.php, element names aren't wrapped with 'entity' till the moment you call prepare();

$myForm->setName('entity');
$myForm->setWrapElements(true);
$myForm->prepare(); // << add this

But I don't believe that it will help, becuase behaviour you describe looks little different. Cau you show us more source code of Entity and var_dumps of things that Aydin and Sina wanted.

0
votes

In ZF2 data is not binded if the form is not valid. The reason because you see an empty string in the return of isValid is that the return type is a boolean, use var_dump instead.