如何将ZF2表单对象序列化为JSON?

时间:2022-10-09 17:45:57

I'm using zf2 form object on the server and ajax code on the client to implement my registration form. I post the form values in the ajax request, no problem, and the form gets them fine with

我在服务器上使用zf2表单对象,在客户机上使用ajax代码实现我的注册表单。我将表单值放在ajax请求中,没有问题,表单可以很好地处理它们

$form->setData($request->getPost());

After I validate the form and perform the registration on the server, I want to send the form back to the client, especially if there are errors, so I can show them to the user. I'm looking for a standard way using zend or any plugin to serialise the form object into JSON format, so I can send it in the response to the AJAX call. Any idea?

在验证表单并在服务器上执行注册之后,我希望将表单发送回客户机,特别是如果有错误,那么我可以将它们显示给用户。我正在寻找一种使用zend或任何插件将表单对象序列化为JSON格式的标准方法,以便在AJAX调用的响应中发送它。任何想法?

1 个解决方案

#1


3  

Well what you can do is run the validation on your form and after that you will return your form within a new JsonModel.

你能做的就是在你的表单上运行验证,然后你会在一个新的JsonModel中返回你的表单。

Here is a little example of how to handle your controller:

这里有一个如何处理控制器的小例子:

class RegistrationController extends AbstractActionController 
{
    public function RegisterAction()
    {
        $form = new RegisterForm();
        $form->setInputFilter(new RegisterInputFilter());

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

            if($form->isValid()) {
                // Handle your registration as the form is valid!
                // return to some path after registration is complete.
                // Show user he registered succesfully, etc. ;)
            }
            // Checks if the request is from JavaScript
            if($this->getRequest()->isXmlHttpRequest()) {
                return new JsonModel(array('registerForm' => $form));
            }
        }
        return new ViewModel(array('registerForm' => $form));
    }
}

Notice that the form object is holding all the invalid inputs including its message after validation.

注意,表单对象保存了所有无效输入,包括验证后的消息。

I would take another approach just to completely render the ViewModel again so you can display the validation message much easier. On the side you could add Client Side (Javascript) validation as it's much more user-friendly, but that is just some fancy shizzle I would do ;) In case of rendering the ViewModel:

我将采用另一种方法来再次完全呈现ViewModel,这样您就可以更容易地显示验证消息。另一方面,您可以添加客户端(Javascript)验证,因为它对用户友好得多,但这只是我想做的一些花哨的工作;)在渲染视图模型时:

use Zend\View\Renderer\PhpRenderer;

if($this->getRequest()->isXmlHttpRequest()) {
    $renderer = new PhpRenderer;
    $registerViewModel = new ViewMOdel();
    $registerViewModel->setTemplate('view/register.phtml');
    return new JsonModel(array('registerViewModel' => $renderer->render($registerViewModel));
}

Note that not setting a template to your viewModel will result in ZF2 getting the default of the action (view/moduleName/registration/register.phtml) you are in! So in your case you don't need to use PhpRenderrer::setTemplate(). But I just hand it to you so you can change it if you are using any other file.

注意,没有为您的viewModel设置模板将导致ZF2获得您所在的操作的默认值(view/moduleName/registration/register.phtml) !因此,在您的示例中,不需要使用PhpRenderrer::setTemplate()。但是我把它交给你,如果你使用其他文件,你可以修改它。

So now you will receive Json from our controller, in your javascript. Retrieve the new ViewModel from Json and remove the old ViewModel and replace it with the new. By removing the old, you also remove any Javascript that is bound to any element within the viewModel, so you might set the events on your body within your javascript or have it on your attributes in Form/RegistrationForm.

现在你将从我们的控制器中,在javascript中接收Json。从Json中检索新的ViewModel,并删除旧的ViewModel,并将其替换为新的。通过删除旧的,您还可以删除任何绑定到viewModel中任何元素的Javascript,因此您可以在Javascript中设置您的主体上的事件,或者在表单/注册表单中将其设置到您的属性上。

Hope this pushes you in the right direction.

希望这能把你推向正确的方向。

#1


3  

Well what you can do is run the validation on your form and after that you will return your form within a new JsonModel.

你能做的就是在你的表单上运行验证,然后你会在一个新的JsonModel中返回你的表单。

Here is a little example of how to handle your controller:

这里有一个如何处理控制器的小例子:

class RegistrationController extends AbstractActionController 
{
    public function RegisterAction()
    {
        $form = new RegisterForm();
        $form->setInputFilter(new RegisterInputFilter());

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

            if($form->isValid()) {
                // Handle your registration as the form is valid!
                // return to some path after registration is complete.
                // Show user he registered succesfully, etc. ;)
            }
            // Checks if the request is from JavaScript
            if($this->getRequest()->isXmlHttpRequest()) {
                return new JsonModel(array('registerForm' => $form));
            }
        }
        return new ViewModel(array('registerForm' => $form));
    }
}

Notice that the form object is holding all the invalid inputs including its message after validation.

注意,表单对象保存了所有无效输入,包括验证后的消息。

I would take another approach just to completely render the ViewModel again so you can display the validation message much easier. On the side you could add Client Side (Javascript) validation as it's much more user-friendly, but that is just some fancy shizzle I would do ;) In case of rendering the ViewModel:

我将采用另一种方法来再次完全呈现ViewModel,这样您就可以更容易地显示验证消息。另一方面,您可以添加客户端(Javascript)验证,因为它对用户友好得多,但这只是我想做的一些花哨的工作;)在渲染视图模型时:

use Zend\View\Renderer\PhpRenderer;

if($this->getRequest()->isXmlHttpRequest()) {
    $renderer = new PhpRenderer;
    $registerViewModel = new ViewMOdel();
    $registerViewModel->setTemplate('view/register.phtml');
    return new JsonModel(array('registerViewModel' => $renderer->render($registerViewModel));
}

Note that not setting a template to your viewModel will result in ZF2 getting the default of the action (view/moduleName/registration/register.phtml) you are in! So in your case you don't need to use PhpRenderrer::setTemplate(). But I just hand it to you so you can change it if you are using any other file.

注意,没有为您的viewModel设置模板将导致ZF2获得您所在的操作的默认值(view/moduleName/registration/register.phtml) !因此,在您的示例中,不需要使用PhpRenderrer::setTemplate()。但是我把它交给你,如果你使用其他文件,你可以修改它。

So now you will receive Json from our controller, in your javascript. Retrieve the new ViewModel from Json and remove the old ViewModel and replace it with the new. By removing the old, you also remove any Javascript that is bound to any element within the viewModel, so you might set the events on your body within your javascript or have it on your attributes in Form/RegistrationForm.

现在你将从我们的控制器中,在javascript中接收Json。从Json中检索新的ViewModel,并删除旧的ViewModel,并将其替换为新的。通过删除旧的,您还可以删除任何绑定到viewModel中任何元素的Javascript,因此您可以在Javascript中设置您的主体上的事件,或者在表单/注册表单中将其设置到您的属性上。

Hope this pushes you in the right direction.

希望这能把你推向正确的方向。