Conditional validation on model dependent on value of model attribute - ruby-on-rails

I'm struggling with validating a postal_code and using a different regex depending on what the country_code attribute is. I tried the following, but it does not work:
class Venue < ActiveRecord::Base
...
attr_accessible :postal_code
attr_accessible :country_code
...
validates_presence_of :country_code
validates_length_of :country_code, :maximum => 2, :allow_nil => true
validates_inclusion_of :country_code, :in => %w( US CA AU GB )
validates_presence_of :postal_code
validates_length_of :postal_code, :maximum => 10, :allow_nil => true
validates_format_of :postal_code, :with => %r{^\d{4}$},
:message => "should be in the format 1111",
:allow_nil => true,
:if => :country_code == 'AU'
validates_format_of :postal_code, :with => %r{^\d{5}([\-]\d{4})?$},
:message => "should be in the format 11111 or 11111-1111",
:allow_nil => true,
:if => :country_code == 'US'
validates_format_of :postal_code, :with =>%r{^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$},
:message => "should be in the format A1A 1A1 (try making letters capitalized)",
:allow_nil => true,
:if => :country_code == 'CA'
# ridculously long (but thorough) regex courtesy of Wikipedia (http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom)
validates_format_of :postal_code, :with => %r{^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})$},
:message => "invalid postal code (try making letters capitalized)",
:allow_nil => true,
:if => :country_code == 'GB'
Am I totally off the reservation on this, or is it just the format of the :if options that are hosed up?

For reference, the :if => parameter accepts a method rather than a condition, you can for example do
validate_presence_of :foo, :if => :country_is_gb?
def country_is_gb?
country_code == 'GB'
end
But as mentioned, lengthy or complex validations are best done in a seperate custom validator.

Take a look at this screencast http://asciicasts.com/episodes/211-validations-in-rails-3
I think its just about time to write a custom validator function for your postal code.

Related

rspec failing:expected 1 errors_on,got 3

class Card < ActiveRecord::Base
belongs_to :trade_type,:class_name => 'TradeType',:foreign_key => 'trade_type_id'
belongs_to :restaurant,:class_name => 'Restaurant',:foreign_key => 'restaurant_id'
validates_presence_of :card_no,:message => '卡号不能为空'
validates_numericality_of :card_no,:message => '卡号必须是数字'
validates_length_of :card_no,:is => 16,:message => '卡号必须是16位'
validates_uniqueness_of :card_no,:message => '卡号不能重复'
end
describe Card do
it 'is valid with card_no,name'do
card = Card.new(
card_no: '1054321239876456',
name: 'zhangsan',
)
expect(card).to be_valid
end
it 'is invalid without a card_no' do
card = Card.new(card_no:nil)
expect(card).to have(1).errors_on(:card_no)
end
end
The reason you are seeing 3 errors instead of 1 is because Card.card_no is blank. That means it is also not numeric and also not 16 characters long.
i.e. all three of
validates_presence_of :card_no,:message => '卡号不能为空'
validates_numericality_of :card_no,:message => '卡号必须是数字'
validates_length_of :card_no,:is => 16,:message => '卡号必须是16位'
are failing.
If you want numericality and length_of to only be validated when the card number is present, add allow_blank: true.
ie.
validates_presence_of :card_no,:message => '卡号不能为空'
validates_numericality_of :card_no, :allow_blank => true, :message => '卡号必须是数字'
validates_length_of :card_no, :is => 16, :allow_blank => true, :message => '卡号必须是16位'

Rails 3 Validation :presence => false

Here's what I expected to be a perfectly straightforward question, but I can't find a definitive answer in the Guides or elsewhere.
I have two attributes on an ActiveRecord. I want exactly one to be present and the other to be nil or a blank string.
How do I do the equivalent of :presence => false? I want to make sure the value is nil.
validates :first_attribute, :presence => true, :if => "second_attribute.blank?"
validates :second_attribute, :presence => true, :if => "first_attribute.blank?"
# The two lines below fail because 'false' is an invalid option
validates :first_attribute, :presence => false, :if => "!second_attribute.blank?"
validates :second_attribute, :presence => false, :if => "!first_attribute.blank?"
Or perhaps there's a more elegant way to do this...
I'm running Rails 3.0.9
For allowing an object to be valid if and only if a specific attribute is nil, you can use "inclusion" rather than creating your own method.
validates :name, inclusion: { in: [nil] }
This is for Rails 3. The Rails 4 solution is much more elegant:
validates :name, absence: true
class NoPresenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || 'must be blank') unless record.send(attribute).blank?
end
end
validates :first_attribute, :presence => true, :if => "second_attribute.blank?"
validates :second_attribute, :presence => true, :if => "first_attribute.blank?"
validates :first_attribute, :no_presence => true, :if => "!second_attribute.blank?"
validates :second_attribute, :no_presence => true, :if => "!first_attribute.blank?"
use custom validation.
validate :validate_method
# validate if which one required other should be blank
def validate_method
errors.add(:field, :blank) if condition
end
It looks like :length => { :is => 0 } works for what I need.
validates :first_attribute, :length => {:is => 0 }, :unless => "second_attribute.blank?"
Try:
validates :first_attribute, :presence => {:if => second_attribute.blank?}
validates :second_attribute, :presence => {:if => (first_attribute.blank? && second_attribute.blank? )}
Hope that help .

Rails validation if a conditon is met

I have this validation
validates :contact_id, :presence => true, :uniqueness => {:message => 'has an account already.'}
in the application.rb model
All is good but I need to only do this validation if the state is "invalid"
For example in the applications table there is a field called state and if there is a application with a contact_id of a user and the state is "invalid" then this validation should not take effect and should let the user save the application
I believe this should do it:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => :invalid?
def invalid?
state == 'invalid'
end
you could also inline that to:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => lambda{ state == 'invalid' }
Hope this helps.
If you are going to do it when the state is not invalid, then you could do that two ways:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:unless => :invalid?
Or you could change it a bit more and have a valid message, which I might prefer:
validates :contact_id,
:presence => true,
:uniqueness => {:message => 'has an account already.'},
:if => :valid?
def valid?
state != 'invalid'
end
Did you try seeing this railscasts video ? http://railscasts.com/episodes/41-conditional-validations
validates :contact_id, :if => :should_validate_contactid?
def should_validate_contactid?
Check condition
end

How do I stop the others validations to validate when the attribute is not :present in Rails?

What I'm curretly doing is the following:
validates :new_pass,
:presence => {:if => :new_record?},
:confirmation => {:if => :password_not_blank?},
:length => {:within => 6...64, :if => :password_not_blank?}
def password_not_blank?
!new_pass.blank?
end
But that is not DRY, I bet there is a way to skip the validations if the attribute is not present.
Also, there isn't any DSL method for validating? I think it would be cleaner than implementing logic inside hashes...
-- Edit, thanks ^^ --
This is what I got now:
validates :new_pass,
:allow_blank => {:on => :update},
:presence => {:on => :create},
:confirmation => true,
:length => {:within => 6...64}
And just for the record and so no one worries (?), this is a virtual attribute, the actual password is encrypted with a before_save, checking that :new_pass is not blank.
The :allow_nil flag for validates might be of interest. Something like this should work:
validates :new_pass,
:allow_nil => true,
:presence => {:if => :new_record?},
:confirmation => {:if => :password_not_blank?},
:length => {:within => 6...64, :if => :password_not_blank?}
validates :new_pass,:presence => true ,:if => :new_record?
validates :confirmation ,:length =>:within => 6...64, :if => :password_not_blank?

why does rails not accept the :message symbol outside of a nested hash option?

Here is my code. The first commented line works fine; the second doesn't. The documentation is sketchy here - no mention of the :message option under the "validates" method, but "validates_format_of" says :message is fine. (http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates) What is going on here?
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
#validates :title, :length => {:minimum => 10, :message => "help!"}
#validates :title, :length => {:minimum => 10}, :message => "help!"
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
In the first commented line, the message value is an option to the :length parameter and it appears its being ignored. To test try
validates :title, :length => {:minimum => 10, :foobar => "help!"}
and you will find it to works without creating an error.
validates is just a shortcut to default validators. You seem to want to set the validates_format_of :message options, so I think you want something like:
validates :title, length => {:minimum => 10}, :format => { :message => "help!" }
Length validations are baked in:
   validates_length_of :title, :minimum => 10, :message => "help!"
Active Record Validations

Resources