Search code examples
javascriptruby-on-railsvalidationsimple-formclient-side-validation

client_side_validation gem - boolean as check boxes


I'm using the client_side_validation gem to do js validations in my app. I'm using the simple_form version.

I've got a boolean attributes in my permission model which are validated

 class Permission < ActiveRecord::Base
   validates :local, :regional, :national, :inclusion => {:in => [true, false]}
 end

However in the view they are rendered as checkboxes, which have value of either 1 or 0, so they are never marked as validated.

Any ideas?


Solution

  • You will have to set :validate => false for checkboxes. E.g.

    <%= simple_form_for @permission, :validate => true do |f| %>
      <%= f.input :name, :label => 'Name' %>
      <%= f.label :local %>
      <%= f.check_box :local, :validate => false %>
      <%= f.label :national %>
      <%= f.check_box :regional, :validate => false %>
      <%= f.label :regional %>
      <%= f.check_box :national, :validate => false %>
      <%= f.button :submit %>
    <% end %>
    

    As checkboxes are by definition true or false, client side validation is not really necessary. With your server-side validation, you are still protected from someone trying to maliciously submit non-boolean values.