Hi this is my User model
class User < ActiveRecord::Base
has_many :events
validates :cellphone, numericality:{ only_integer: true, message:"no es un numero"}, format: { with: /\d{11}/, message: "mal formato, deben ser 11 digitos, incluyendo codigo de area" }, :allow_blank => true
validates :phone, numericality:{ only_integer: true, message:"no es un numero"}, format: { with: /\d{11}/, message: "mal formato, deben ser 11 digitos, incluyendo codigo de area" }, :allow_blank => true
validates_numericality_of :cellphone, :on => :create, :message => "no es un numero", :allow_blank => true
validates_numericality_of :document, :on => :create, :message => "no es un numero", :allow_blank => true
validates :name, :presence => true
validates :lastname, :presence => true
validates :document, :presence => true, :uniqueness => true
validates :cellphone, :presence => true, :uniqueness => true
validates :phone, :presence => true, :uniqueness => true
validates_format_of :email,:with => Devise::email_regexp, :allow_blank => true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
I want to valida, example :cellphone, numericality only if it pass :cellphone, :presence => true validation.
I already read all the post here in StackOverflow but i cant seem to understand what they are doing.
If some1 could help me with this step by step.
Thanks
:allow_blank => true
:presence => true
As #apneadiving mentioned in the comment, these two are 180 degree apart from each other. If one is true, the other one should be false. You can't have both at the same time.
What you are asking is that you want to check for the numericality if and only if the record is presence. For that, you can do the following:
validates :cellphone, numericality:{ ... }, format: { ... }, :allow_blank => true
In this way, since cellphone field can be blank, numericality will only be checked for it if it exists.
The other option is: You can pass a block to check if the field exists, and it does, check for numericality.
Here's how:
validate :cellphone, numbericality: { ... }, :if => lambda{ |object| object.cellphone.present? }
Related
class Stadium < ActiveRecord::Base
validates :name, :presence => true
validates :city, :presence => true
validates :contructiondate, :presence => true
validates :capacity, :presence => true
validates :image, :presence => true
validates :name, :uniqueness => true
validates :city, :uniqueness => true
validates :capacity, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :image, :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix, :message => "Uniforme Invalido", :multiline => true }
validates :city, :inclusion => { :in => %w(Belo Horizonte Brasilia Curitiba Fortaleza Manaus Natal Recife Rio de Janeiro Salvador Sao Paulo), :message => "%{value} no esta permitido" }
validate :mydate_is_date?
def mydate_is_date?
errors.add(:contructiondate, 'must be a valid date') if !contructiondate.is_a?(Date)
end
end
I have a problem with inclusion, It works perfectly with words like Natal, Manaus, Salvador, but with words like "Sao Paulo", "Rio de Janeiro" does not work, How could I fix it?
Thanks
Try escaping the spaces with a backslash \.
Without escaping:
> %w(Belo Horizonte Brasilia Curitiba Fortaleza Manaus Natal Recife Rio de Janeiro Salvador Sao Paulo)
=> ["Belo",
"Horizonte",
"Brasilia",
"Curitiba",
"Fortaleza",
"Manaus",
"Natal",
"Recife",
"Rio",
"de",
"Janeiro",
"Salvador",
"Sao",
"Paulo"]
With escaping:
> %w(Belo Horizonte Brasilia Curitiba Fortaleza Manaus Natal Recife Rio\ de\ Janeiro Salvador Sao\ Paulo)
=> ["Belo",
"Horizonte",
"Brasilia",
"Curitiba",
"Fortaleza",
"Manaus",
"Natal",
"Recife",
"Rio de Janeiro",
"Salvador",
"Sao Paulo"]
Right now, I have a User model with a username field that's being validated by:
validates :username,
:presence => true,
:length => { :in => 3..60 },
:format => { :with => /^[a-zA-Z0-9\-_ ]+$/ }
How can I hide the :length and :format validation errors if :presence is not met?
Try :allow_blank => true in 2nd and 3rd validations.
I think you can do like this:
validates :username,
:presence => true,
:length => { :in => 3..60, :allow_nil => true },
:format => { :with => /^[a-zA-Z0-9\-_ ]+$/, :allow_nil => true }
It will not care about length and format validations when username is not set, but it will work fine with at least one character typed.
Some validations are repetitive in my models:
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
How would I put that in a mixin? I get this error if I just put 'em in a mixin
app/models/validations.rb:5: undefined method `validates' for Validations:Module (NoMethodError)
module Validations
extend ActiveSupport::Concern
included do
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
end
end
The validates macro must be evaluated in the context of the includer, not of the module (like you probably were doing).
Your module should look something like this:
module CommonValidations
extend ActiveSupport::Concern
included do
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
end
end
Then in your model:
class Post < ActiveRecord::Base
include CommonValidations
...
end
I'm using ActiveSupport::Concern here to make the code a little clearer.
I have an object that contains a number range and a description [min_val, max_val, name].
I need to validate that min_val < max_val. However, if one of them is blank I get a nil comparison error, instead, I'd like to tell the user that a number is required.
Also, how can I change the error message for numericality?
validates :min_val, :presence => true, :numericality => {:greater_than => 0, :less_than => :max_val}
validates :max_val, :presence => true, :numericality => {:greater_than => 0, :greater_than => :min_val}
validates :name, :presence => true, :if => Proc.new { |r| !r.min_val.nil? || !r.max_val.nil? }
You can use :message to specify a custom error message.
validates :max_val, :presence => true, :numericality => {:greater_than => 0, :message => " is an invalid number."}
validates :min_val, :presence => true, :numericality => {:greater_than => 0, :message => " is an invalid number."}
validate do |record|
record.errors.add_to_base("The min_val should be less than max_val") if min_val.to_i >= max_val.to_i
end
validates :name, :presence => true, :if => Proc.new { |r| !r.min_val.nil? || !r.max_val.nil? }
I'm a newbie in rails. how put a validated on password field. specified only on create and used an allow_blank method. but everytime i update it still creates a nil in the password field. any help?
validates :password, :presence => { :on => :create },
:confirmation => true,
:length => { :within => 8..40},
:allow_blank => true,
Try this:
validates :password, :presence => { :if => :new_record? },
:confirmation => true,
:length => { :within => 8..40 }