简体   繁体   中英

Validates presence of THIS or THAT

I have a form where the user is prompt to enter a title and either :this or :that . A user can't enter both fields.

<% f.input :title%>
<% f.input :this %>
<% f.input :that%>

for my :title i have in my Model

validates :title, :presence => true

How can i pass a validation for either :this or :that

You can do this

validates :that, :presence => true, :if => Proc.new {this.blank?}
validates :this, :presence => true, :if => Proc.new {that.blank?}

Wouldn't just the first line be sufficient?

validates :that, :presence => true, :if => Proc.new {this.blank?}

If 'this' is blank and so is 'that', the first line would fail validation, so you wouldn't need the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM