Validation for one or another field

Hello everyone, in my model I have amongst other fields, the :variant_id
and
:cart_id, I want to validate their presences but in my app the user only
needs to set one of them, so I can’t make it validates_presence_of
:variant_id, :cart_id.

I am sure there’s a solution for that, please tell me if you know!

Thanks!

Hello everyone, in my model I have amongst other fields, the :variant_id and
:cart_id, I want to validate their presences but in my app the user only needs to
set one of them, so I can’t make it validates_presence_of :variant_id, :cart_id.

I am sure there’s a solution for that, please tell me if you know!

You can either write a custom validation method that returns false if
both are unset or you could add some :if => {…} code to your
validation line so it only validates one field if the other is blank.
You’d need that on both validation lines and your error message would be
a little confusing suggesting the user needs to pick both.

If it was me, I’d go custom so I could put up a reasonable error
message.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs