ajax - How to serialize ZF2 form object to JSON? -


I am using a zf2 form object on the server and ajax code on the client to implement my registration form. I post the prices of the form in the AJAX request, there is no problem, and form them

  $ form-> Fixes with set data ($ request-> getPost ());  

After validating the form and registering on the server, I want to send the form back to the customer, especially if there are errors, so I can show them to the user. I'm looking for a standard way to sort the form object in JSON format or using a plugin, so I can send it in response to an AJAX call. Any ideas?

What can you do well on your form of verification and then you come back Your Form within

New JasonModel

A small example of how to handle your controller:

  class registration controller extends AbstractActionController {public function register (action) {$ form = new RegisterForm ( ); $ Form-> SetInputFilter (new register input filter ()); If ($ this-> getRequest () -> isPost ()) {$ form-> SetData ($ this-> getRequest () - & gt; getPost ()); If valid ($ form-> isValid ()) {// your registration as a form is valid! // Return to some path after the registration is completed // show that the user was successfully registered, etc.;)} // check if the request is from javascript ($ this-> getRequest () -> isXmlHttpRequest ()) {New JSONOLODE (array ('registerForm' =) & Gt; $ form)); }} Return new look modal (array ('register form' => $ form)); }}  

Note that the form object is holding all invalid inputs, including validation after verifying.

I will take another approach to re-present the complete ViewModel again so that you can display the verification message very easy. On the edge you can add client side recognition (JavaScript) because it is more user-friendly, but it is just some fancy shake;) ViewModel in the case of providing code>:

  Use Zend \ View \ Renderer \ PhpRenderer; If ($ this-> getRequest () -> isXmlHttpRequest ()) {$ renderer = new Php Redder; $ RegisterViewModel = New ViewMOdel (); $ RegisterViewModel- & gt; SetTemplate ('see / register.phtml'); New JsonModel (array ('registerViewModel' = & gt; $ renderer- & gt; Render ($ registerViewModel));}  

Note that your viewModel As a result, ZF2 gets the default action ( View / ModulationName / Registration / Register.Afile )! In your case you do not have to use PhpRenderrer:: SetTemplate () . But I just hand it over to you so that you can change it if you are using any other file.

Now you Json You will get our controller, in your javascript, from the new ViewModel to retrieve Json and delete the old ViewModel and replace it with By removing the old, you can also remove any JavaScript that is bound to any element within viewModel , so that you can set your body events within your javascript Can it be in your properties or form / Registration form .

Hope this puts you in the right direction.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -