Rails - 验证参数取决于其他参数

时间:2015-06-25 05:46:12

标签: ruby-on-rails validation

我有两个字段mobile_notelephone_no。我想用以下条件验证这些字段:

If mobile_no is present on submit, then
  don't validate telephone_no


if telephone_no is present on submit, then
  don't validate mobile_no

我该怎么做?

1 个答案:

答案 0 :(得分:4)

你可以在你的模型中尝试这个:

validates :mobile_no, presence: true, unless: :telephone_no?
validates :telephone_no, presence: true, unless: :mobile_no?