views:

44

answers:

1

Form validation with jQuery is as easy as adding a classname to a field. Form validation with rails is as easy as putting a condition in to your controller (and/or model).

I figure there should be a way to write the validations once and have them applied both client and server side. I've always been a fan of writing my own javascript, but with rails3 unobtrusivity UJS might be well worthwhile here if it can accomplish this.

Thanks!!

A: 

You should look at creating your own form builder for customising the behaviour of form_for. You could do something that sets the class to the name of the validations defined on the attribute and have jQuery bind itself to the respective class names. Let's start with what the form builder might look like.

class ValidationFormBuilder < ActionView::Helpers::FormBuilder
  def text_field(object_name, method, options = {})
    options[:class] = object_name.class.validators_on(method).map do |k| 
      # Eg: ActiveModel::Validations::PresenceValidator -> presence
      k.to_s.slice(/[^:]+Validator$/).chomp('Validator').downcase
    end.join(' ')
    super(object_name, method, options)
  end
end

You'll need to setup form_for to use the ValidationFormBuilder.

<%= form_for @foo, :builder => ValidationFormBuilder do |f| %>
  <%= f.text_field :bar %>
<% end %>

... becomes something like

<form action="/foo" method="post">
  <input type="text" class="presence" name="foo[bar]" id="foo_bar">
</form>

If you need more flexibility over the class names, you might want to create a hash that maps to the desired string.

class ValidationFormBuilder < ActionView::Helpers::FormBuilder
  MAPPINGS = {
    ActiveModel::Validations::PresenceValidator => 'text'
  }

  def text_field(object_name, method, options = {})
    options[:class] = object_name.class.validators_on(method).map do |k| 
      MAPPINGS[k]
    end.join(' ')
    super(object_name, method, options)
  end
end

You can see the complete list of validations included in Rails by peeking in activemodel/lib/active_model/validations of the Rails source code. I hope that's enough to get you started.

Tate Johnson