I recently wrote a new gem called realtime-validations. It is now
covering our use case, but it sure can be improved, and I will
personally improve and maintain it if there are unmet needs for any of

- Purpose
- Validate user input at client-side
- Never, ever replicate logic.

- Why this gem ? We already have at least the great
client-side-validations gem !
- We like client-side-validations gem, however we believe that there
is a 'even better' way to handle client side validations.
- If you have a simple model in which you have simple validations
(length, presence, even uniqueness...) client-side-validations will
serve you well. However, things start to become hard if you have
custom validators that do really strange fu, or if you have, for
example, a :uniqueness scoped by user_id. Realtime-validations comes
to fix this issues.
- On complex (and probably strange) validations you need to
replicate the server logic on javascript (at the client). This is hard
to maintain, and someone can forget to update one or the other.
Replicated logic in two different languages is not the best way to go.
Also you would need to jump over language specific issues.

- How is it done ?
- Each time the user blurs a field (that is, focuses out a field), a
request is sent to a special controller, that will create (or find a
model), set the edited property and validate the model. The errors of
that field are then sent back to the client.

- Benefits
- You only have validations on your model, where it should be. It
does not matter how strange or complicated they are, they are just
issued without problems.
- You can also send to the server special information as which is
the current user or similar stuff, so the model will set this
information before setting the edited field and validate it.
Validations with :uniqueness :scoped_by => :user_id will work nicely.

Gem: http://rubygems.org/gems/realtime-validations
Code: https://github.com/ereslibre/realtime-validations
Example: https://github.com/ereslibre/realtime-validations-examples

So please, if you need something else that should be achieved I would
be happy to know about it.

Best regards,
Rafael Fernández López

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
postedFeb 8, '12 at 3:19p
activeFeb 8, '12 at 3:19p

1 user in discussion

Rafael Fernández López: 1 post



site design / logo © 2022 Grokbase