simple_form & bootstrap validations not working

家住魔仙堡 提交于 2019-12-08 00:45:01

问题


I want to setup a 'Create Account' page. The gems I'm using are:

  • rails (3.2.3)
  • simple_form (2.0.1)
  • omniauth-identity
  • twitter-bootstrap-rails (2.0.6)
  • mongoid (2.2.3)

The form looks as follows:

= simple_form_for @identity, :url => '/auth/identity/register', :html => { :class =>    'form-horizontal' } do |f| 
  = f.input :name, :input_html => {:name => 'name'}
  = f.input :email, :input_html => {:name => 'email'}
  = f.input :password, :as => 'password', :input_html => {:name => 'password'} 
  = f.input :password_confirmation, :label => "Confirm Password", :as => 'password', :input_html => {:name => 'password_confirmation'} 
  .form-actions
    = f.button :submit, 'Sign Up', class: 'btn-primary'
    = link_to 'Cancel', root_path,  class: 'btn-danger'

The corresponding Identity model is

class Identity
  include Mongoid::Document
  include OmniAuth::Identity::Models::Mongoid
  field :name, :type => String
  field :email, :type => String
  field :password_digest, :type => String

  validates_presence_of :name, :email
  validates_uniqueness_of :email
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
end

If I don't fill in any of the text field of the form and then click on 'Sign Up', the form does not display any error messages from the validators, but instead redirects me to the home page!

Am I missing something obvious or could this be some problem with the gem versions I am using? I could of course implement the same form without simple_form using twitter-bootstrap markup, but would prefer it this way.


回答1:


I'm in a similar situation where simple_form does validate a field in another model, but not in the user model.

In any case, I noticed you don't have:

validates_presence_of :password

in your model. Maybe mongoid saves you from doing that step? I don't know mongoid, but for me that is the magic that validates that the field is not blank. For the field that is working for me I have this:

validates_presence_of :title, :localeLanguage, :message => "can't be blank"



回答2:


I had this problem, I fixed it by including this in all my simple forms, I then find that it renders properly:

<%= f.error_notification %>


来源:https://stackoverflow.com/questions/10374749/simple-form-bootstrap-validations-not-working

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