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.
Related
I get this error
Unknown validator: 'MessageValidator'
I have no idea why I'm getting that.
What's wrong with my code?
validates :title,
:presence => true,
:uniqueness => true,
:length => { :maximum => 100 },
:message => "Must be input and has to be less than 100 characters, and unique."
Try:
validates :title,
:presence => {:message => "Title can't be blank." },
:uniqueness => {:message => "Title already exists."},
:length => { :maximum => 100, :message => "Must be less than 100 characters"}
I assume you want the message to be when the length validation fails. You should be including message in that hash like:
validates :title,
:presence => true,
:uniqueness => true,
:length => { :maximum => 100, :message => "Must be less than 100 characters"}
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'm trying to make a simple custom validation message. The validation I'm using compiles and runs fine, but I don't see any change in the message:
validates :rating, :inclusion => { :in => 0..5 }, :presence => { :message => " must be within 0-5" }
The message I get is still Rating is not included in the list
I need to validate that rating is present and is a decimal between 0-5
Alright, I solved it. This is the validation that works:
validates :rating, :inclusion => { :in => 0..5, :message => " should be between 0 to 5" }
validates :rating, :presence => { :message => " cannot be blank" }
and I added this
validates :rating, :numericality => { :message => " should be a number" }
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 }