开发者

validations for certain actions in model

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_开发者_Go百科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:

class Whatever < ActiveRecord::Base
  attr_accessor :enable_strict_validation

  validates_presence_of :name # this always happens
  validates_uniqueness_of :name, :if => :enable_strict_validation
end

Then somewhere else (eg your controller), you can do:

@whatever = Whatever.new(...)
@whatever.save  # <= will only run the first validation

@whatever.enable_strict_validation = true
@whatever.save  # <= will run both validations


Validations are not handled by the controller and as such are not action specific.

Validations can, however, be limited based on the type of update being made to the model. Update, Create, or Save.

Would it work for you to limit the validations only to new records?

validates_numericality_of :card_cvv, :on => :create

If not, you can write custom validators to handle returning true on conditions you specify (such as the controller action), but again it isn't really the "Rails Way".

The simplest example would be using the validate method

validate do
  return true if my_action_is_redeem

  self.card_cvv =~ /^\d*$/
end

For more info on validations see the docs


you can restrict the validate on the create (new) or on the update (redem operation is a update?).

your validation could be like this:

validates_presence_of :attribute1, :on => :update #so, only on redem method is validated)

You can choose when to validate, on this events: :create, :update, :save (both create or update)

More info on: http://guides.rubyonrails.org/active_record_validations_callbacks.html#on

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜