我试图仅在用户单击提交按钮时在表单中显示错误,但是当前,它在用户单击提交按钮之前显示所有错误。如何仅在用户提交表单时显示错误?
我在Rails中使用简单格式
这是我的简单格式:
<div class="col-md-10 col-lg-8 col-xl-5 col-md-offset-4 mx-auto">
<%= simple_form_for @customer,url: customers_path,method: :post do |f| %>
<%= f.error_notification %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email,input_html: { autocomplete: 'email' } %>
<%= f.input :budget,collection: ["€200,000 - €299,999","€300,000 - €399,"€400,000 - €499,"€500,000 - €649,"€650,000 - €799,"€800,000 - €1,000,000","€1,000 +"] %>
<%= f.input :comments,:as => :text,:input_html => { 'rows' => 10,'cols' => 10 } %>
<%= f.button :submit,"Submit",class: "btn-primary trigger mt-1" %>
<% end %>
</div>
这是我在客户模型中的客户验证:
class Customer < ApplicationRecord
validates :email,presence: true,format: { with: URI::MailTo::EMAIL_REGEXP },uniqueness: true
validates :first_name,length: { minimum: 2 }
validates :last_name,length: { minimum: 2 }
validates :budget,presence: true
validates :comments,presence: true
end
谢谢