Zend Framework 2 - Wrapped form elements cause validation to fail

和自甴很熟 提交于 2019-12-11 11:23:57

问题


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.


回答1:


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"
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/15759119/zend-framework-2-wrapped-form-elements-cause-validation-to-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!