Rails, Simple Form, and Client Side Validations aren't working

眉间皱痕 提交于 2019-12-12 00:25:46

问题


My model:

class Hotel < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :room_types
validates :user_id, presence: true
validates :name, presence: true, length: { in: 2..15 }
end

The form code:

    <%= simple_form_for @hotel, :validate => true do |f| %>
    <%= f.input :name %>
    <%=  f.submit 'Create Hotel' , :class => 'btn btn-primary', :type => 'submit' %>
   <% end %>

There doesn't seem to be any client side validation being performed. I can submit the form without filling in any information. Any ideas?

EDIT: Ok, I looked at the source of my page and it has this error:

window['new_hotel'] = {"type":"SimpleForm::FormBuilder","error_class":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.error_class is deprecated and has no effect. (called from error_class at (eval):1)","error_tag":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.error_tag is deprecated and has no effect. (called from error_tag at (eval):1)","wrapper_error_class":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.wrapper_error_class is deprecated and has no effect. (called from wrapper_error_class at (eval):1)","wrapper_tag":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.wrapper_tag is deprecated and has no effect. (called from wrapper_tag at (eval):1)","validators":{"hotel[name]":{"presence":{"message":"can't be blank"},"length":{"messages":{"maximum":"is too long (maximum is 15 characters)"},"maximum":15}}}};

回答1:


You have to set up browser_validations option in your config/initializers/simple_form.rb file

# Tell browsers whether to use default HTML5 validations (novalidate option).
# Default is enabled.
config.browser_validations = true

PS Don't forget to run

$ rails g simple_form:install

to generate initializer.



来源:https://stackoverflow.com/questions/12306811/rails-simple-form-and-client-side-validations-arent-working

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