Validation of Zend_Form(Ajax version)

intro
This article shows source code that validates data using Zend_Form and Ajax. It also uses Dojo library.
You can see the screens from the demonstration site.

1.Create Controller
Create the file application/constorllers/IndexController.php with the following content.
2.Create View
Create the file application/views/scripts/index/profile.phtml with the following content.

3.Check
Let’s access the web server and check the validation using Zend_Form and Ajax.
Reference
About the method processAjax of Zend_Form, Please see the manual.

History
Date Content
2008/4/28 Published
2008/4/28,29 Bugs in javascript of view were fixed.
2008/4/30 Added reference about processAjax method.

14 Comments

  1. Hi!
    I’m trying to use this example. It’s ok while validation has errors. I can see the error messages and etc.
    But when all fields is correct, i just can see “OK!” alert window. No data submitted in this case.
    Can you help me please? 🙂

  2. Hello.
    I think it’s correct…
    Because after the client program submit the form data, it is validated by the server side program and the result is sent back to the client. If there’s no error, the client program will show the “OK” message.
    If you pointed out another problem, please comment again.
    Thank you.

  3. There’s some ways to load javascript dynamically.
    If you can use jquery, you can do it with the following code.
    $(‘#some-container’).html(script_html);
    If you can’t, please search another way according to your environment.
    Thank you.

  4. I am in search for a nice Zend Framework integration with a total one-page ajax solution where many forms are loaded and unloaded directed by a mainmenu just like native apps. The problem is to place the javascript code in the right places.
    I have researched many options like jQuery, packaging and dynamically loading .js by dojo. I cant find nice example that describe its integration. Do you have some?

  5. Thank you for your comment.
    I don’t know about a nice Zend Framework integration too.
    But I think you can create your own view helper.
    If you want to use Zend_Dojo_View_Helper_ContentPane, create your contentpane helper just replacing ‘dijit.layout.ContentPane’ with ‘dojox.layout.ContentPane’ of Zend_Dojo_View_Helper_ContentPane and change the class and function name.
    Because dojox.layout.ContentPane allows to execute scripts.
    It will be like the following.

    And you need to add view helper path with the following code.

    In your view, you can use the above helper like the following.

    If you use layout, you need to disable it manually.

  6. Thanks for the tutorial.

    I have got it going very well. But when I put in a CAPTCHA, processAjax() doesn’t validate it. Would you please tell me how to validate CAPTCHA as well?

    Ivan

  7. I think it is difficult to use processAjax() in that case.
    But you can replace all form elements like the following code.
    (I’m not sure that it is practical.)

  8. Hello oplabo,

    In echo to what elGringo wrote, I’m getting a hard time understanding when the form is actually processed once it’s validated. On the client side, an ‘OK’ message is displayed.
    But when does the server process the data to perform actions on it?

    In your example, the profileAction() method of the controller is only called once at the beginning, and is never called when all the fields are validated.

    Am I missing something?

  9. Thanks for the tutorial. It’s great.

    Prototype people read this:

    For the people who wish to do the same thing in prototype JS, add these lines when you receive the response (the prototype AJAX request will be regular one and need no special modifications),

    var response = t.responseText.evalJSON();
    json = eval( ‘(‘ + response + ‘)’ );

    and then change all the ‘dojo.byId’ to $ . That’s it. Hope this will be useful to someone.

    Thanks,

    Sathesh

  10. Hi!
    I’m trying to use this example. It’s ok while validation has errors. I can see the error messages and etc.
    But when all fields is correct, i just can see “OK!” alert window. No data submitted in this case.
    Can you help me please? 🙂

Leave a Reply

Your email address will not be published.