我正在尝试将客户端验证集成到我的rails应用程序中。我需要验证才能出现在模态中。我目前正在将这个宝石用于表格:
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>
用户模型
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
答案 0 :(得分:0)
对于客户端验证,您需要使用Javascript。客户端验证需要在它到达服务器之前进行验证,您设置的验证是Rails验证以及在表单提交之后发生的验证。如果要在提交表单之前验证它,我建议使用Jquery插件,例如http://jqueryvalidation.org/。将Jquery验证添加到项目中将允许您在将表单下载到服务器之前检查它们。