Mini John
Mini John

Reputation: 7941

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

Upvotes: 0

Views: 91

Answers (2)

user3088130
user3088130

Reputation: 51

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.

Upvotes: 0

techvineet
techvineet

Reputation: 5111

You can do this

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

Upvotes: 1

Related Questions