Search code examples
jqueryruby-on-rails-3client-side-validationjquery-select2

rails v3 with select2 gem ignoring client_side_validation error message


I'm using rails 3.0.9, client_side_validation gem and just tried to use select2 gem by using thier js and css file manually

Downloaded the files from https://github.com/ivaynberg/select2/tags

A simple form with a presence validation

    <%= javascript "validations/rails.live.validations", "select2.min"%>
    <%= stylesheet "select2"%>
    <script type="text/javascript" charset="UTF-8">
        $(document).ready(function() {
            $("#user_tag_ids").select2();
             });
    </script>
    <%= form_for @user, :validate => true do |f| %>
        <%= f.label :tag_ids %><%= mark_required(@user, :tag_ids) %>
        <%= f.collection_select(:tag_ids, @hobbies, :id, :display_name, {:prompt => "please select"}, :multiple => true) %>
        <%= f.submit %>
    <% end %>

Now the select box UI changed as expected,

But when the cursor is focused out without selecting any value, then client_side_validation gem should display a error message but no message is displayed (not even the error_field class is set to that fields).

Does select2 gem ignores/skiping to render the error message ?

If I click submit button then only i'm getting (client_side_validation gem) error message.

Any idea to solve this?


Solution

  • Add validation to the current settings object in javascript. Example

    var validators = window[$('#place_order_form').attr('id')].validators
    
    var input_name = $("#user_tag_ids").attr('name');
    validators[input_name] = {"presence":{"message":"Must be filled"}}
    

    Reference: https://github.com/bcardarella/client_side_validations/issues/149