[Agavi-Users] Extract validation for use with Javascript

39 views
Skip to first unread message

Jeramy Wenserit

unread,
Jan 21, 2009, 12:13:27 PM1/21/09
to Agavi Users Mailing List
Afternoon,

I've been asked to enquire whether the following concept is possible,
we have some realtime Javascript validation on a web-form, but instead
of having the error messages stored in two locations (in the js script
and in the validation's action.xml file), is it possible to centralise
these messages just into the Agavi's module/<modulename>/validate/
action.xml file.

Then in the input view class, have the XHTML function call (i thinking
out loud) request the validation xml file, the file name could be
passed by the action to the view with an attribute setting or so.

Can I use the validation Manager in the action/view to gain all the
possible error messages, then parse them to JSON and print it to a JS
variable or so.

...or should I use Agavi Configuration interface to load in the
validation xml file and return a structure PHP data type/object which
can then be parsed into JSON.

Cheers folks,

Jeramy Wenserit (E_mE)
e: evercl...@gmail.com

_______________________________________________
users mailing list
us...@lists.agavi.org
http://lists.agavi.org/mailman/listinfo/users

David Zülke

unread,
Jan 21, 2009, 1:46:34 PM1/21/09
to Agavi Users Mailing List
Hi Jeramy,

this ("how can I do client-side validation, like, as people type,
using server-side validators") is a question that has been brought up
before many times, with no useful answer yet.

However, I think I just had a nice idea on how to do that.

Basically, what you want to do is submit the contents of the form via
XMLHTTPRequest in the background, and get back any validation errors
from the server. However, if all is good, then it would run the
Action, which in 99% of the cases is *not* what you want.

Hence you need to be able to run validation without running the action.

Which, as it turns out, is wonderfully simple to do.

<validator class="equals" source="headers" required="false">
<argument>X-Requested-With</argument>
<ae:parameter name="value">lolcats</ae:parameter>
</validator>

This validator will only run if the X-Requested-With HTTP header is
sent in the request, and in that case, it will always fail, because
all the JS frameworks send "XMLHTTPRequest" as the value there.

Then, you can
a) use executeJson() in your error view to collect validation info
from the validation manager and return that to the browser, where you
insert it using javascript, or
b) load a simple layout with just html and body elements in the
decorator (don't forget the base href!) and switch on FPF there
(ajaxHtml might be a better output type here), then re-insert the body
contents into the document (but that's tricky, with field focus etc,
especially as people are typing).

In any case, it's a good way of running the validation while making
sure the action is not executed.

Cheers,

- David

P.S: if you just want to submit forms using ajax or so, then you don't
really need anything like that, because that "just works"... I was
assuming that you want to periodically check if the entered values are
valid while the user is filling the form, or while he is typing, or
whenever he changes to a new form field or so.

Craig Fairhurst

unread,
Jan 22, 2009, 1:12:38 PM1/22/09
to Agavi Users Mailing List
Jeramy

Interesting you raise this topic, as of December last year I have been tackling this problem head on.

The Project:
Automatic Form Generation with Client and Server Side Validation

Key Problems:
Extensibility
PHP Object Structure
Keeping complete control of the Style/Layout of the form independently of the PHP Object
Validation Syntax - what to base it upon?

Ongoing Concerns:
Outputting validation based upon answers provided in the form

Solutions:
Extensibility - Use only one function for adding form items and determine what field type to process from the first parameter 
PHP Object Structure - Based upon W3C DOM Form Markup; form fields are registered to form fieldset. Form fieldset contains own properties.
Style/Layout -  Combination of the correct XHTML Form Markup with unique ID's on the key element containers gives complete  Style and Layout control through CSS
Validation Syntax - Pseudo Syntax based mainly upon the Mootools FormCheck sytax, due to lack of docs and knowledge on validators.xml

Key Features:
Form Generation and Validation is done from inside the Base View for the Project Module. The function can then be called from inside the view itself.

Working Example
The project is undergoing development and is being used within a site I'm currently working on, see link below.

gosnewsUsersBaseView.class.php

RegisterInputView.php

RegisterInput.php

I've not progressed any further with fields such as Image Captcha and so on but if there was enough community support this could potentially be a good open-source forge project for people to contribute additional features and options. 

Thoughts???

Craig Fairhurst

unread,
Jan 22, 2009, 1:32:01 PM1/22/09
to Agavi Users Mailing List
Resulting object of this register form can be seen below..

Updated the link: database info was incorrect apologies...

Jordi Boggiano

unread,
Jan 23, 2009, 8:11:28 AM1/23/09
to Agavi Users Mailing List
I don't mean to ruin any effort here, I mean if you like it that's all
good, but imo Agavi's validation xml and the FPF fully compensate for
the lack of "form classes", and I don't feel the need to use those
(whatever lib you use) at all. What I have been thinking about though
is automatically creating javascript validators based on the
validation xml. That way you keep full control over the html of your
form that you just define in the templates, but validation is fully
automatic all the way.

So if anyone is interested in working (or you have already something
you're willing to share) on a FPF-JSedition, drop me a mail.

Cheers,
Jordi

Reply all
Reply to author
Forward
0 new messages