问题
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