Rails上的Simple_form_for-提交后如何显示错误验证 解释:

我试图仅在用户单击提交按钮时在表单中显示错误,但是当前,它在用户单击提交按钮之前显示所有错误。如何仅在用户提交表单时显示错误?

我在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

谢谢

abcd123abc123 回答:Rails上的Simple_form_for-提交后如何显示错误验证 解释:

您可以按以下方式更改simple_form_for(第2行)

<%= simple_form_for @customer,html: { novalidate: true },url: customers_path,method: :post do |f| %>

解释:

通过添加,html:{novalidate:true} ,此选项可向表单添加新的novalidate属性,指示其跳过所有HTML 5验证。

本文链接:https://www.f2er.com/2853874.html

大家都在问