How to add custom validation messages depending on other attribute values?

I am using Ruby on Rails 3.2.9 and I am just trying to use the state_machine stone. I have the following statements:

# Model class attributes are: # # [String] status # [Boolean] checkin_1 # [Boolean] checkin_2 # class Article < ActiveRecord::Base state_machine :attribute => :status, :initial => :unconfirmed do state :confirmed, :value => 'confirmed' state :unconfirmed, :value => 'unconfirmed' event :confirm do transition :unconfirmed => :confirmed end event :unconfirm do transition :confirmed => :unconfirmed end end end 

I would like to add custom check messages for instance objects (avoiding saving these objects) when the confirm and checkin_1 and / or checkin_2 are false . That is, given that I'm trying to execute confirm each of the following objects:

 <#Article id: 1, :status: 'unconfirmed', checkin_1: false, checkin_2: false> <#Article id: 1, :status: 'unconfirmed', checkin_1: false, checkin_2: true> <#Article id: 1, :status: 'unconfirmed', checkin_1: true, checkin_2: false> 

then I would like to avoid saving objects and add accordingly error messages like the following:

 "can not be confirmed if it is not checked for 1 and 2" "can not be confirmed if it is not checked for 1" "can not be confirmed if it is not checked for 2" 

How can I do it?

+4
source share
2 answers

How to do it:

 state confirmed do validate do if !checkin_1 and !checkin_2 msg = "can not be confirmed if it is not checked for 1 and 2" errors.add(:checkin_1, msg) errors.add(:checkin_2, msg) elsif !checkin_1 errors.add(:checkin_1, "can not be confirmed if it is not checked for 1") elsif !checkin_2 errors.add(:checkin_2, "can not be confirmed if it is not checked for 2") end end end 
+1
source
  class Article < ActiveRecord::Base attr_accessor :checkin_1 attr_accessor :checkin_2 state_machine :attribute => :status, :initial => :unconfirmed do state :confirmed, :value => 'confirmed' state :unconfirmed, :value => 'unconfirmed' before_transition any => :confirm do |article| return false unless article.custom_validate? end event :confirm do transition :unconfirmed => :confirmed end event :unconfirm do transition :confirmed => :unconfirmed end end private def custom_validate? validate_flag = true if self.checkin_1 and self.checkin_2 msg = "can not be confirmed if it is not checked for 1 and 2" errors.add(:checkin_1, msg) errors.add(:checkin_2, msg) validate_flag = false elsif !checkin_1 errors.add(:checkin_1, "can not be confirmed if it is not checked for 1") validate_flag = false elsif !checkin_2 errors.add(:checkin_2, "can not be confirmed if it is not checked for 2") validate_flag = false end validate_flag end end 
0
source

All Articles