3
votes

I have a model User

class User < ActiveRecord::Base
  validates :goal, :presence => true, :on => :update
  belongs_to :goal, :class_name => Goal, :foreign_key => "goal_id"
  ...
end

and on the form i have

<%= simple_form_for @users,:remote => true, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }, :validate => true do |f| %>
 <%= f.association :goal,:label => "My overall goal is clearing", :input_html => {:class => 'goals'},:include_blank => true %>

But the rails client side validation does not pick up the validation on the association, it lets me fill in blank for goal.

Does rails client side validation gem work for simple form association?

I am getting the below script on my html page

if (window.ClientSideValidations == undefined) window.ClientSideValidations = {};
if (window.ClientSideValidations.forms == undefined) window.ClientSideValidations.forms = {};
window.ClientSideValidations.forms['edit_user_14'] = {
    "type": "SimpleForm::FormBuilder",
    "error_class": "error",
    "error_tag": "span",
    "wrapper_error_class": "field_with_errors",
    "wrapper_tag": "div",
    "wrapper_class": "input",
    "wrapper": "default",
    "validators": {
        "user[mobile_number]": {
            "format": [{
                "message": "should be 10 digits",
                "with": /^\d{10}$/i
            }]
        },
        "user[profile_pic]": {
            "integrity": [{
                "message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.integrity"
            }],
            "processing": [{
                "message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.processing"
            }],
            "download": [{
                "message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.download"
            }],
            "format": [{
                "message": "Wrong file format",
                "with": /\.(gif|jpeg|jpg|png)$/i,
                "allow_blank": true
            }]
        }
    }
};
2
Have you uncommented the code block in your config/initializers/client_side_validations.rb? Is your rails.validations.js loading in your layout? - Ricardo Castañeda
@Cadence96 yes i just updated the question with the script that gets loaded - codeAnand
Are you using jQuery gem? Have you tested it in a different and non associated model? Your code is different than the one I have from my rails.validations.js v3.1.4 - Ricardo Castañeda
@Cadence96 i use 3.2.0 .... its works on non associated models... only the association is not working - codeAnand
I tested it in an association, using :on => :update it failed. - Ricardo Castañeda

2 Answers

1
votes

The key distinction here is using validates :goal_id ... not validates :goal ...

Since this is an association, the real property on your ActiveRecord model (and in the underlying data store) is goal_id. That's what client_side_validations is looking for by default.

0
votes

Looks like ClientSideLavodation gem can't find proper validation for association so just add

validates :goal_id, :presence => true, :on => :update

to your model.