Rails 3 UJS dry Client + Server Side form validation

这一生的挚爱 提交于 2019-12-11 03:53:11

问题


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


回答1:


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.




回答2:


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




回答3:


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('');



回答4:


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



来源:https://stackoverflow.com/questions/3952117/rails-3-ujs-dry-client-server-side-form-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!