Reusing Zend validators on client-side

1.1k views Asked by At

I found this question on another forum where it didn't get any answers. I thought it was a good question worth asking here. I'm pasting it here word for word.

Hi guys, I've got a pie in the sky idea, and I'm just wondering if it's even remotely possible, or if I should give up and try something else.

I've got a bunch of Zend_Dojo_Form forms with a bunch of custom validators on the Zend side - they work fine, the form errors out when values are incorrect, it's all working exactly as you'd expect, however, what I'd also like is for the same validators to work on the Dojo client side of the form - but without having to completely re-implement them, validator by validator.

I understand that at its simplest level, the Dojo isValid method doesn't know or care about the application behind it, and indeed for it to even be able to get access to the errors in the application it'd have to attempt a submit, which isn't what it's meant to do, but has anyone tried a system of something like:

  1. override form onSubmit to add our code
  2. test Dojo isValid()
  3. if the Dojo side is happy, AJAX submit the form
  4. if successful, which would be returned i don't know how, then forward on to the appropriate page
  5. if not successful, then (and this is where it gets tricky I guess) pass back enough data to know what field errored out, what the error was, and then call enough of the Dojo isValid internal guts to make the form look like it didn't validate?

Sorry to just be airing ideas like this, I just figured it might be really useful if we could come up with a way to sort of globally add custom validators to our forms that work both client and server side, without having to implement them uniquely on both sides.

2

There are 2 answers

0
Tjorriemorrie On BEST ANSWER

Meh, dojo :(

Use Jquery :)

Look at this, it's awesome: Ajaxify Your Zend_Form Validation with jQuery

We’re going to take what was put together in the last 3 videos and now include some server-side validation that will appear asynchronously. This is an example of using Zend_Form as a validation tool via JSON.

2
Arnaud Le Blanc On

Zend_Form has a processAjax() method that can be used to validate any or all of a form's elements and return a JSON-encoded result (true, or a list of errors).

So this can be used to pre-validate all or part of the form in the onsubmit event handler; or even to pre-validate fields individually in their onchange event hander.