I have a field called visit_time with two distinct values. They are "AM" and "PM"
I check the presence of the visit_time by the following validation syntax.
validates_presence_of :visit_time,
message: "visit time is required"
Then I need to check the inclusion validation only if the visit_time is presence, for this I am using the Proc. But it is not working.
validates :visit_time,
:inclusion => { :in => [ 'AM', 'PM'],
:message => "%{value} is not a valid time" },
:if => Proc.new { |o| o.errors.empty? }
Let me know what's wrong on it. Is Proc is not working for inclusion ??? Thanks in advance.
If you want the inclusion validation to run only if it's present, you should change the Proc to this instead:
if: Proc.new { |o| o.visit_time.present? }
Related
I am trying to do model validation for gstno only if the selected country equals India. I tried like this not working:
validates :gstno, uniqueness: true, :format => {:with => /[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}/, :message => 'INCORRECT FORMAT!'} if self.country =='IN'
The error is like this:
undefined method `country' for #<Class:0x007fb021d6a0c8> Did you mean? count
Can you please try the following code:
validates :gstno, uniqueness: true, :format => {:with => /[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}/, :message => 'INCORRECT FORMAT!'}, if: -> { country == 'IN' }
You probably want to move this to a custom validation method
https://guides.rubyonrails.org/active_record_validations.html#custom-methods
self will be available to you there
I have in my model
class Account < ActiveRecord::Base
validates_length_of :amount, :in 1..255, :on => update, :if => Proc.new { |a| false if a.id == nil;a.amount.blank? }
validates_length_of :name, :in 1..255, :on => update, :if => Proc.new { |a| false if a.id == nil;a.name.blank? }, :unless => user_has_amount?
end
when I comment out the if condition, it works fine but with them validation fails which is confusing. I know that the validation should only run if the proc returns true or unless returns false
in my controller I have
#account.update_attributes({:name => "king", :amount => "1223"}
the save fails and when I check errors I get
#account.errors.details
{:name =>[{:error=>:too_short, :count=>1}], :amount =>[{:error=>:too_short, :count=>1}]}
Strong Params are not an issue because I have
def self.params
params.require(:account).permit!
end
I have no idea why it fails though the value are present and correct.
Try this the following:
class Account < ActiveRecord::Base
validates :amount, length: { in: 1..255 }, on: :update
validates :name, length: { in: 1..255 }, on: :update
end
Check your strong parameters. Your error tells you that something is wrong before you get to validation: :name =>[{:error=>:too_short, :count=>1}] This is saying that the minimum string count is 1 but that your string is not even that long. Here is the docs on that.
You can try: Account.find(1).update_attributes({:name => "king", :amount => "1223"} to see if #account is not being set properly.
You can also customize the language on your errors to help further:
validates_length_of : name, within: 1..255, too_long: 'pick a shorter name', too_short: 'pick a longer name'
The issue that was happening was for one reason. The code was inheriting from a class that had:
slef.reload
in one of the function that were being called during validations.
so every time we try to update the update failed because once that validation was hit it reloaded the original values from database and ignored new values which if old values are empty is just a head scratcher.
Thanks everyone for the help
I am getting this error when when I try to validate. I am trying to validate whether if the string is not in the DB.
this is my model
class Location < Locations::Location
validate do
#strong URL check for url_prefix
errors.add(:url_prefix, "URL already taken") if self.url_prefix.valid? && is_on_web;
end
end
Instead use,
validates :url_prefix, :uniqueness => { :message => "URL already taken and is online" }
update:
conditional validation can be added to solve your second problem like this,
validates :url_prefix, :uniqueness => { :message => "URL already taken and is online" }, :if => :is_on_web?
I have a Project model and I need to test if the billing_address_type is valid.
class Project < ActiveRecord::Base
validates :billing_address_type, :inclusion => { :in => %w(h o) }
def billing_address_types
options = {"Home" => "h", "Organisation" => "o"}
if person.present?
options.delete("Home") if person.address.blank?
options.delete("Organisation") if person.organisation.blank?
end
options
end
The validates line is wrong, however. I need to check for inclusion of the hash values returned by the method billing_address_types.
How can I check for the hash values only?
Thanks for any help...
You can pass lambda or a new Proc to the :in option which will be dynamically evaluated, and use the values method on the hash returned from billing_address_types to get the hash values only:
validates :billing_address_type, :inclusion => { :in => lambda { |a| a.class.billing_address_types.values } }
See the documentation for details.
I'm trying to set up my model in Rails 3.2.8 such that particular values must be present, but are allowed to be the empty string. Does anyone know how to do this?
The behavior I'm looking for looks like:
#foo.value = "this is a real value"
#foo.valid? # => true
#foo.value = nil
#foo.valid? # => false
#foo.value = ""
#foo.valid? # => true
If I use
validates :foo, :presence => true
then I get what I want for 1 and 2, but not 3. And, helpfully, :allow_blank => true is ignored when validating presence.
I also tried
validates :foo, :length => { :minimum => 0 }, :allow_nil => false
Same behavior: get what I want for 1 and 2, but not 3.
I suppose I could just mark the column as NOT NULL in the database, but then I have to deal with catching the exception; I'd really rather catch this at the validation stage.
Any ideas?
I would try something like this:
validates :foo, presence: true, unless: lambda { |f| f.foo === "" }