使用 'bootstrap_form' gem 将客户端验证添加到模态

Adding client side validations to modal using 'bootstrap_form' gem

我正在尝试将客户端验证集成到我的 rails 应用程序中。我需要验证出现在模态中。我目前正在使用这个 gem 作为表格:

https://github.com/bootstrap-ruby/rails-bootstrap-forms

我无法使验证工作,并且我认为我设置不正确,或者需要使用不同的工具。如果有人能提供建议,我将不胜感激。

这是一个表单示例:

HTML

<div id="signupmodal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
                  <h3 class="">Sign Up</h3>
            </div>
            <div class="modal-body">
              <%= bootstrap_form_for :new_user, :html => {:id => 'new_user'}, url: users_path do |f| %>
              <%= f.text_field :name, hide_label: true, placeholder: "Enter Your Name", icon: "tag", :class => "input-lg required" %>
              <%= f.text_field :email, hide_label: true, placeholder: "Enter Your Email", icon: "user", :class => "input-lg required" %>
              <%= f.password_field :password, :html => {:id =>'user_signup_password'}, hide_label: true, placeholder: "Enter Your Password", icon: "lock", :class => "input-lg required" %>
              <%= f.password_field :password_confirmation, hide_label: true, placeholder: "Confirm Your Password", icon: "lock", class: "input-lg required"%>
              <%= f.submit "Signup", class: "btn btn-primary btn-lg btn-block signup-submit" %>
              <% end %>
              <div class="text-center signup-or"> OR </div>
              <%= link_to 'Signup with Facebook', '/auth/facebook', :class => 'btn btn-primary btn-lg btn-block' %>
            </div>
        </div>
    </div>
</div>

User Model

class User < ActiveRecord::Base
  has_secure_password

  before_create :confirmation_token

  belongs_to :state
  belongs_to :country
  belongs_to :account_type

  has_many :authentications

  validates :email,
    presence: true,
    uniqueness: {case_sensitive: false},
    format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }

  validates :password, :presence =>true, :confirmation =>true
  validates_confirmation_of :password
end

对于客户端验证,您将需要 Javascript。客户端验证需要在它到达服务器之前进行验证,您设置的验证是 Rails 验证,并且在提交表单之后发生。如果您想在提交之前验证表单,我推荐一个 Jquery 插件,例如 http://jqueryvalidation.org/ 。添加 Jquery 验证到您的项目将允许您在将表单提交到服务器之前检查它们。