![](/img/trans.png)
[英]Trouble using bootstrap_form gem - couldn't find file 'rails_bootstrap_forms' error
[英]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>
用戶模型
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驗證將使您可以在將表單替換為服務器之前檢查表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.