【问题标题】:How to make these variables `required`如何使这些变量成为“必需”
【发布时间】:2015-12-16 04:08:04
【问题描述】:
<%= form_for @organization do |f| %>
  <%= render partial: 'shared/error_messages', locals: { object: f.object } %>

  <label id="icon" for="country"></label>
  <%= f.select :country, [['Afghanistan', 'AF'],
                          ['Albania', 'AL'],
                          # etc.,
                          ['Zimbabwe', 'ZW']
                         ], {:prompt => 'Please select location', required: true} %>

  <label id="icon" for="id_number"></label>
  <%= f.text_field :id_number, placeholder: 'e.g.: 123abc' %>
  <%= f.check_box(:no_id) + "&nbsp <i>No ID</i>".html_safe %>
<% end %>

在注册表单中,我有上面的代码。我怎样才能为这个表单实现以下目标?

1) 如何使country 成为必填项? 尽管required: true 没有选择国家,但目前它确实接受它。也许它也将“请选择位置”视为一个值?

2) 如何使其需要输入id_number 或选中no_id 的复选框,并且不能同时使用两者。我希望对表单以及模型级别有​​此要求。

更新:我在下面为第二个要求添加了模型验证,这似乎有效:

validate  :id_or_no_id

def id_or_no_id
  if (self.id_number.nil? || self.id_number.empty?) && self.no_id != true
    errors.add(:id_number, "Need id or need to select checkbox")
  elsif (self.id_number && !self.id_number.empty?) && (self.no_id && self.no_id == true)
    errors.add(:id_number, "Can't have both id and checked checkbox")
  end
end

如何让country 的表单验证起作用?如果我将f.select 的最后一行更改为], {}, {:required =&gt; true, :placeholder =&gt; 'Select your location...'} %&gt;,下拉列表顶部会出现一个空行框(所以占位符不工作),如果没有选择一个国家,那么表单验证的工作原理是它要求选择一个国家。但是现在占位符不起作用...

【问题讨论】:

  • 我假设您使用的是 Rails。表单中的字段是否与模型相关联?你能显示整个 form_for 块吗?答案可能是您会使用模型验证(请参阅guides.rubyonrails.org/active_record_validations.html),但需要了解更多才能确定。
  • 重读您的问题意味着您可能还需要一些 javascript。但如果控制器设置为提供帮助,则不一定。
  • 谢谢,我确实使用 Rails 4 并且表单绑定到模型(添加到 OP)。 country 验证是我在表单级别而不是在模型级别想要的东西(在其他表单中,国家/地区是不需要)。

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


【解决方案1】:

确保您的模型经过验证

class Model
  validates :country, presence: true
  validates :id_or_no_id
  private
  def id_or_no_id
    if #custom logic here#
      errors.add :some_field, "you must have a valid id or no id"   
    end
  end
end

编辑

根据您的编辑,您的函数应如下所示,以便更好地编码 (这也是丑陋的代码)

validate  :id_or_no_id
def id_or_no_id
  if (id_number.nil? || id_number.empty?) && !no_id
    errors.add(:id_number, "Need id or need to select checkbox")
  elsif (id_number && id_number.present?) && no_id
    errors.add(:id_number, "Can't have both id and checked checkbox")
  end
end

也许这也有效

validate :use_one_id

def use_one_id
  if !id_number && !no_id
    errors.add :id_number, "you must select one...."
  end

  if id_number && no_id
    errors.add :id_number, "can't select both"
  end
end

【讨论】:

  • 谢谢。 country 验证我只希望在表单级别而不是在模型级别(在其他表单中,国家/地区是 not 必需的)。我已将私有方法添加到我的 OP。
  • 那么你需要custom_validate_it。只需编写自己的验证函数,您可以在其中检查它是否需要
  • 在一般情况下,有一个模型有时需要一个有效的国家或地区,这听起来像是一种糟糕的 Rails 方法,它不是 Rails 风格的
  • 原因是免费账户和付费账户的区别,只有付费账户需要country(交易需要)。
  • 是否不能使用required: true 使country 仅用于此表单?我知道如果没有将此类要求也添加到模型验证中,有人可以轻松破解此问题,但在这种特殊情况下没关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多