JS based easy Field Validations in Rails

Posted by anup.narkhede on January 06, 2008

Most of you will disagree with this post to use Javascript based form validations in Rails. However this is an attempt to simplify integration of DEXAGOGO - Really easy field validations in Rails. This is one of the most structured and easy validation technique written by Andrew Tetlaw and team. (Demo)

To use it in Rails, copy simple_validation.rb in application lib directory and add following lines to config/environment.rb file.

Download and Import validation.js along with prototype.js in layout from here.

Two methods are introduced to render forms.

  • validated_form_for
  • validated_remote_form_for

Usage:

:class attribute refers to validation rule (or combination of rules) and :title attribute is the validation message. Details of all rules are available at http://tetlaw.id.au/view/javascript/really-easy-field-validation

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Jim Cropcho Sun, 06 Jan 2008 12:13:08 PST

    “Most of you will disagree with this post to use Javascript based form validations in Rails.”

    I don’t, depending what your needs are. Client-side form validation can greatly reduce the amount of POSTs you app has to handle (and reduce your bandwidth costs, by extension).

    At the same time, the current design methodology of such systems makes validation logic a lot less DRY, because ORM-layer validations are specified in model classes, and client-layer validations are specified in view documents (i.e. rHTML files)- a separate problem in itself.

    A DRY, elegantly-designed client-side validation system would store validation logic which is shared between both layers in a single location, while also allowing rules which only apply to one layer to be specified, as well.

  2. Tarun Chandel Mon, 28 Jan 2008 02:18:27 PST

    Hi Anup

    I like you blog, especially because I am trying to learn RoR and I was just collecting some resources for getting started. Your blog will surely be of help, keep up the good work :)

    Tarun

Comments