满足条件时跳过某些字段的验证

时间:2015-03-07 01:28:48

标签: jquery ruby-on-rails ruby forms validation

嗨,我正在尝试做以下事情。

我正在制作一个结帐页面,我有一个这样的表格

  <%= form_for([@ofert, @order], builder: FoundationFormBuilder) do |f| %>

    <%= f.fields_for :invoice_address do |builder| %>
        <%= render partial: 'invoice_address_fields', locals: { f: builder } %>
    <% end %>

    <%= f.check_box :invoice_equals_shipping, checked: true, label: 'My Invoice address is the same Shipping address' %>

    <%= f.fields_for :shipping_address do |builder| %>
      <%= render partial: 'shipping_address_fields', locals: { f: builder } %>
    <% end %>

    .....
    <% end %>

由de fault检查复选框“我的发票地址是相同的送货地址”,默认情况下使用jquery隐藏“shipping_address”字段。

这两个字段在开头都是空的,因此用户通常会填写invoice_address字段并选中“我的发票地址与发货地址相同”复选框,问题是隐藏字段“shipping_address”是验证“字段不能为空”

如何在选中复选框时验证该字段是否验证?

1 个答案:

答案 0 :(得分:3)

您可以在模型中向validates添加条件。类似的东西:

validates :shipping_address, presence: true, unless: :invoice_equals_shipping?