I am encountering a problem which I have never encountered before. I am working on code that was written by another programmer and it is kind of a mess.
Here is the problem. I have the following validations in the my model :
validates_presence_of :subscription_level,
:message => 'please make a selection'
validates_presence_of :shipping_first_name
validates_presence_of :shipping_last_name
validates_presence_of :shipping_address
validates_presence_of :shipping_city
validates_presence_of :shipping_state
validates_presence_of :shipping_postal_code
validates_presence_of :shipping_country
validates_presence_of :billing_first_name
validates_presence_of :billing_last_name
validates_presence_of :billing_address
validates_presence_of :billing_city
validates_presence_of :billing_state
validates_presence_of :billing_postal_code
validates_presence_of :billing_country
validates_presence_of :card_number
validates_numericality_of :card_number
validates_presence_of :card_expiration_month
validates_numericality_of :card_expiration_month
validates_presence_of :card_expiration_year
validates_numericality_of :card_expiration_year
validates_presence_of :card_cvv
validates_numericality_of :card_cvv
I have two actions for the controller in question. One is new and the other is redeem.
I want to perform all of these validations with the new action but want to skip most of them for redeem action.
The problem I am facing right now is that using valid? in the controller is also validating things which are not required for redeem action.
How can I get around this?
It’s hacky, but I’ve had to resort to having an attribute flag that can enable/disable validations in certain states. (My specific example was a multi-page form, where we eventually want to validate all required fields for an object, but we only can validate data that has been submitted on previous pages)
Here’s an example of how that might look:
Then somewhere else (eg your controller), you can do: