我在具有引导模式(用于登录/注册)的应用程序上工作,其中执行客户端验证。当您尝试使用db中的常规但不存在的电子邮件登录时,会执行服务器端验证,并且您将被重定向到users / signin页面。
因此,我需要在客户端进行验证,并在模态中显示错误消息,而不是重定向。问题是,我第一次在真实的大应用程序上工作,我无法找到执行这些验证和重定向的地方。我找不到任何相关的JS文件或控制器动作..
用户模型:
validates :password, presence: true, :on => :create
validates :password, confirmation: true, :on => :create
validates :email, presence: true
validates :username, uniqueness: true, presence: true
validates_format_of :username, :with => /^[a-z\d]*$/, :message => "Just letters and numbers please"
注册模式:
#modal-registration.modal.hide.fade{"aria-hidden" => "true", "aria-labelledby" => "registrationLabel", role: "dialog", tabindex: "-1"}
.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", type: "button"} ×
%h3#myModalLabel Registration
.modal-body
.row-fluid
.span6
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: "form", id: "registration-form" }, :validate => true) do |f|
.control-group
.controls
= f.input :email, autofocus: true, label: false, placeholder: 'email'
.control-group
.controls
= f.input :username, autofocus: true, label: false, placeholder: 'Username'
.control-group
.controls
= f.input :password, label: false, placeholder: 'Password'
.control-group
.controls
= f.input :password_confirmation, label: false, placeholder: 'Password confirmation'
.control-group
.controls
= f.button :submit, "Registration"
.span6
= link_to "Registration via Facebook", "#", class: "btn btn-large btn-block"
= link_to "Registration via LinkedIn", "#", class: "btn btn-large btn-block"
.modal-footer
%button.btn{"aria-hidden" => "true", "data-dismiss" => "modal"} Close
Client_side_validation jQuery:
jQuery ->
$("#modal-registration, #modal-sign-in").on "shown", ->
$(ClientSideValidations.selectors.forms).validate()
使用过的gems:devise,client_side_validations,client_side_validations_simple_form,bootstrap,haml。
答案 0 :(得分:0)
您是否尝试过rails bootstrap forms gem?
您可以轻松地使用rails bootstrap forms gem进行客户端验证
您只需添加remote: true
代码,例如:
= bootstrap_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :remote => true) do |f|
= f.email_field :email
= f.text_field :username
= f.password_field :password
= f.password_field :password_confirmation
= f.submit