Feature Request: Client side validation classes for forms

133 views
Skip to first unread message

Karthik Abinav

unread,
Feb 3, 2012, 12:46:50 PM2/3/12
to django-d...@googlegroups.com
Hi,

  I was thinking about a feature that could be implemented. For common fields like username having only alphanumeric , or phone numbers having only numbers, a client side validation need not be written every time.Instead one could directly write something like,

forms.CharField(validator = "usernamevalidation")

in the forms definition and the client side validation for that field would automatically be taken care of by the validator class. This will save a lot of time while making large websites with lot of registration forms and in general be helpful to people who dont really know javascript and yet want some amount of frontend validation in place.


Regards,
Karthik Abinav

Adrian Holovaty

unread,
Feb 3, 2012, 4:03:46 PM2/3/12
to django-d...@googlegroups.com

I like the idea of having a JavaScript version of form validation.
Basically we could make a view class that takes a Form object in
__init__() and returns JSON of the errors in a consistent way -- this
would be very easy to do. Then we could provide some standard
JavaScript to parse that JSON and add the error messages to the
appropriate fields in the form in a consistent way.

Good idea! It's a bit too late now to add it to Django 1.4, but I'd
like to implement this for the next version.

Adrian

Łukasz Rekucki

unread,
Feb 3, 2012, 4:57:32 PM2/3/12
to django-d...@googlegroups.com

I don't get it. That would require sending the data to the server
(right?), so it's not
really client-side. I think http://www.dajaxproject.com/ does exactly that.

What I would like to see instead is providing HTML5 attributes for
standard fields and
making it easier to add ones to custom ones. Some simple to implement
ones are: "required", min/max for number fields, max_length for a
textarea. Regular expression can be supported via "pattern" (with some
code to translate them to JS regexp syntax).

--
Łukasz Rekucki

Henrique Bastos

unread,
Feb 3, 2012, 6:17:52 PM2/3/12
to django-d...@googlegroups.com, django-d...@googlegroups.com
Django's form could use a more high level solution for both client side form validation and asynchronous form validation.

Djaxproject.com is ok, but is too ad hoc. It would be nice to have Adrian's and Łukasz perspectives combined somehow.

[]'s,
--
Henrique Bastos
+55 21 9618-6180
http://henriquebastos.net
http://twitter.com/henriquebastos

> --
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To post to this group, send email to django-d...@googlegroups.com.
> To unsubscribe from this group, send email to django-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
>

Karthik Abinav

unread,
Feb 4, 2012, 5:53:50 AM2/4/12
to django-d...@googlegroups.com

@Łukasz : Thanks a lot for the response. But as Henrique mentioned using the __init__()
in the view class and then returning a JSON response would be server side right ? Or Am i missing something here ? 

                I was thinking something on the lines of this. Add an attribute in django.forms.fields which will include the js media having the client side validation ? 
Regards,
Karthik Abinav,


Tai Lee

unread,
Feb 6, 2012, 11:31:35 PM2/6/12
to Django developers
I found that Alex's `django-ajax-validation` works pretty well for
this and I think it works the way you described. Perhaps it could be
updated and included into Django core, if there is good support for
it.

https://github.com/alex/django-ajax-validation/

Cheers.
Tai.


On Feb 4, 8:03 am, Adrian Holovaty <adr...@holovaty.com> wrote:
> On Fri, Feb 3, 2012 at 11:46 AM, Karthik Abinav
>

Stephen Burrows

unread,
Feb 8, 2012, 4:39:04 AM2/8/12
to Django developers
The problem I've run into with client-side validation of django
projects is:

1) If you can't replicate every piece of validation the server does,
the user experience will be inconsistent, which is bad.
2) You can't replicate every piece of validation the server does.
Simple example: uniqueness checks. (Well, I suppose that technically
you could include the entire database table in the response and check
uniqueness of whatever field client-side, but I really hope that
sounds like a terrible idea to you.)

Ajax validation would be the way to go; ideally, you would be able to
do it on a per-field basis - but if there were a generic way of doing
it for a form, limiting that form to a subset of fields would be
trivial.

Karthik Abinav

unread,
Feb 8, 2012, 5:28:30 AM2/8/12
to django-d...@googlegroups.com
On 7 February 2012 10:01, Tai Lee <real....@mrmachine.net> wrote:
I found that Alex's `django-ajax-validation` works pretty well for
this and I think it works the way you described. Perhaps it could be
updated and included into Django core, if there is good support for
it.

https://github.com/alex/django-ajax-validation/


@Tai: Thanks a lot for the link.Though I havent personally gone through this code yet(I will be doing so in 2 -3 days.) If this behaves exactly as requested by me above then, I think it will be a great idea to include this into the main django repo if there is a good support for this implementation.

 
Cheers.
Tai.


On Feb 4, 8:03 am, Adrian Holovaty <adr...@holovaty.com> wrote:
> On Fri, Feb 3, 2012 at 11:46 AM, Karthik Abinav
>
> <karthikabin...@gmail.com> wrote:
> >   I was thinking about a feature that could be implemented. For common
> > fields like username having only alphanumeric , or phone numbers having only
> > numbers, a client side validation need not be written every time.Instead one
> > could directly write something like,
>
> > forms.CharField(validator = "usernamevalidation")
>
> > in the forms definition and the client side validation for that field would
> > automatically be taken care of by the validator class. This will save a lot
> > of time while making large websites with lot of registration forms and in
> > general be helpful to people who dont really know javascript and yet want
> > some amount of frontend validation in place.
>
> I like the idea of having a JavaScript version of form validation.
> Basically we could make a view class that takes a Form object in
> __init__() and returns JSON of the errors in a consistent way -- this
> would be very easy to do. Then we could provide some standard
> JavaScript to parse that JSON and add the error messages to the
> appropriate fields in the form in a consistent way.
>
> Good idea! It's a bit too late now to add it to Django 1.4, but I'd
> like to implement this for the next version.
>
> Adrian

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.




Regards,

Karthik Abinav,

Karthik Abinav

unread,
Feb 13, 2012, 1:04:51 PM2/13/12
to django-d...@googlegroups.com
This behaves in a somewhat similar manner and as far as I have gone through this and tested, I am pretty happy with this. But I would still prefer having some HTML5 attributes with fallback javascript as well. Since these new html5 features are becoming more and more popular among the web developers, so we must get them into django also as much as possible.Any thoughts ? 
--
Regards,
Karthik Abinav,

Reply all
Reply to author
Forward
0 new messages