Ok, the idea is not complicated, thought. 

This is the inheritance diagram:

Zend_Form --> My_Ajax_Form

Zend_Form_Element --> My_Ajax_Form_Element
Zend_Form_Element_Text --> My_Ajax_Form_Element_Text
...
Zend_View_Helper_FormCheckbox --> My_View_Helper_FormCheckbox

Zend_Ajax_Form has some extra functions:
- renderAjaxMessages    (returns Jquery-formatted messages ready to be
evaluated by javascript client)
- addMessage            (add a message after automatic validation)
- addIdToElements               (if we have more than one form, this will
make the form unique)

The validators and filters remain unchanged as the functionality is
abstracted from ajax.

The key here is override the form view helpers (not all, only some of them),
control the validation from server and always evaluate javascript responses
from server, because it's not all about data but we can receive execution
code too.

The submit view helper will attach a jquery code to submit the form through
an ajax request and receive the response from server.

The action that will validate the form will have the same structure as we
can see in the documentation, but, the response from action needs to change.

Public function init() {
        $this->form = new My_Form_Login();
}

Public function showFormAction() {
        $this->view->form = $form;
}

Public function validateFormAction() {
        $ajax = new Ajax();

        If($this->form->isValid($_POST)) {
                // do whatever and redirect client
                // using javascript
                $ajax->alert('Well done');
                $ajax->redirect($this->view->Url('...'));
        } else {
                $ajax->addContent($this->form->renderAjaxMessages());
        }

        $ajax->render();
}

In this example I'm using my own "Ajax" class, disabling layout and
viewrenderer, so it take control of the response.

The error messages rendered by the Ajax Form are Jquery code, like this:

$('[EMAIL PROTECTED]']).removeClass('formErrorClass');
$('<ul id="error-formid-element"
class="ul-errorClass">').insertAfter('[EMAIL PROTECTED]');
$('[EMAIL PROTECTED]').append('<li class="li-errorClass">Empty
value</li>');
$('[EMAIL PROTECTED]']).addClass('formErrorClass');


So, the ajax server part takes control of the form and he will dictates what
he needs to do in both cases: error (render error messages) or success
(redirect, show message or whatever you need).

I will post the full code soon, 

 

I took Zend_Form and I've added some extra functions: renderAjaxMessages.
I've inherited every Zend_Form_Element too, and each one has the
"renderAjaxMessages" function (inherited from Z

-----Mensaje original-----
De: Benjamin Eberlei [mailto:[EMAIL PROTECTED] 
Enviado el: miƩrcoles, 26 de noviembre de 2008 16:12
Para: fw-general@lists.zend.com
Asunto: Re: [fw-general] Validating JQuery forms via AJAX


Hello,

you could open up an "Enhancement/Feature" issue in Jira for me
(http://framework.zend.com/issues) and attach the changes. I can review and
test them, and we could then think about how to integrate them into ZF. I
am very interested to contribute an ajax validation, it should be very
robust though, so I cannot guarantee that i can include it.

On Wed, 26 Nov 2008 15:36:42 +0100, "Xavier Vidal" <[EMAIL PROTECTED]>
wrote:
> I have an inherited form class from Zend_Form that can validate Ajax
forms
> and render the error messages using JQuery, if someone is interested, i
> can
> share my code and we can promote it to ZF.
> 
> 
> -----Mensaje original-----
> De: Benjamin Eberlei [mailto:[EMAIL PROTECTED] 
> Enviado el: miƩrcoles, 26 de noviembre de 2008 14:50
> Para: fw-general@lists.zend.com
> Asunto: Re: [fw-general] Validating JQuery forms via AJAX
> 
> 
> Hello Manuel,
> 
> there is currently no way to validate your form via ajax unless you
> implement that yourself.
> 
> I might implement that someday, but the form plugin is not part of jQuery
> or jQuery UI, so I havent integrated that yet.
> 
> best regards,
> Benjamin
> 
> On Wed, 26 Nov 2008 02:58:18 -0800 (PST), manuelpedrera
> <[EMAIL PROTECTED]> wrote:
>> 
>> Hi, I've created a ZendX_JQuery_Form which has some fields with
>> validators.
>> The submit's click event is linked to a javascript function, where I
> want
>> to
>> check if the input entered is valid or not.
>> 
>> When I did this using dojo forms, it was easy through form.isValid(),
> but
>> I
>> can't figure the way it's done using JQuery.
>> 
>> Is there a way to use the validators before sending the data to the
>> server?
>> --
>> View this message in context:
>>
>
http://www.nabble.com/Validating-JQuery-forms-via-AJAX-tp20698748p20698748.h
> tml
>> Sent from the Zend Framework mailing list archive at Nabble.com.


Reply via email to