开发者

Rails 3 UJS dry Client + Server Side form validation

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!!


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.


https://github.com/alluniq/validated-fields


You can use server side validation using RJS (and it doesn't depend on you are using UJS or not):

# create.js.haml
= render :partial => "shared/flash_messages", :locals => { :flash => flash }

- if @message.errors.any?
  $('#reply_message').html('#{escape_javascript(render(:partial => "message_form"))}');
- else
  $('ul.data_grid .list').append('#{escape_javascript(render "message", :message => @message)}');
  $('#reply_message textarea').val('');


Does JS validations, including some neat stuff ajax for validating uniqueness.

https://github.com/bcardarella/client_side_validations

http://railscasts.com/episodes/263-client-side-validations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜