2015-06-25 18 views
4

我有2個字段,即mobile_notelephone_no。我想驗證此字段的條件如下:Rails - 驗證參數取決於其他參數

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 

我該怎麼做?

+0

http://stackoverflow.com/questions/6273289/validate-presence-of-field-only-if-another-field-is-blank -rails – webster

+1

感謝您的鏈接。 :) – BartSabayton

回答

4

你可以試試這個模型中:

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

它有效的男人。謝謝! – BartSabayton