I'm adding a validator inside the User.rb model to validate the birthdate given is over 18 years old before create the devise user.
This is the validation I have.
# user.rb
validates :birthdate, :before => lambda { 18.years.ago }, :before_message => "must be at least 18 years old"
Inside the User schema
# User schema
t.date "birthdate"
Problem is that I'm getting the following error when trying to save the object.
ArgumentError in Devise::RegistrationsController#create
Unknown validator: 'BeforeValidator'
Unknown validator: 'BeforeValidator' means you've overestimated the magic :)
Try this:
validate do |user|
if user.birthdate > 18.years.ago
errors.add(:birthdate, "must be at least 18 years old")
end
end
You can also try:
before_validation :validade_birth_date
private
def validade_birth_date
if (birthdate != nil)
errors.add(:birthdate, "must be at least 18 years old") unless (birthdate < DateTime.now)
end
end
:before is not an actual default validator of ActiveRecord. When validates finds any validator name it doesn't know, it searches one custom validator with its name (in this case BeforeValidation)
All validations are made before the actual object is stored by nature, and you don't have to make your own validator as pointed out. ActiveRecord already provides one: :numericality
validates :birthdate, :numericality => { :greater_than => 18 , :message => "must be at least %{count} years old" }
Related
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'm new to RoR. I'm facing a problem when using validates_uniqueness_of. I've a table with 3 columns:
name || father_name || dob
Vimal Raj || Selvam || 1985-08-30
I've a code in my model like this:
class Candidate < ActiveRecord::Base
attr_accessible :dob, :father_name, :name
validates_uniqueness_of :name, scope: [:father_name, :dob], case_sensitive: false,
message: ": %{value} already present in the database!!!"
before_save :capitalize_name, :capitalize_father_name
private
def capitalize_name
self.name.capitalize!
end
def capitalize_father_name
self.father_name.capitalize!
end
end
It throws error as expected when I insert => "vimal raj, Selvam, 1985-08-30"
But it is accepting the following data => "Vimal Raj, selvam, 1985-08-30" . I was expecting it will throw an error, but unexpectedly it accepts the record and inserts into the db as a new record.
Please help me on how to solve this.
If you want a one-liner solution, please try this :
before_validation lambda {self.name.capitalize!; self.father_name.capitalize!}
Hope, it will help.
I think the case_sensitivity is only matching on name, not on father_name. I would try changing before_save to before_validation so that both name and father_name are consistently the same capitalization when your validation is evaluated.
I just updated from rails 2.3 to 3, i'm trying to replace this old method with something cleaner, because it's outputting the model and field name, wtf!
However I get the above error when calling validates_uniqueness_of (the presence works fine). I passed in the primary id scope, and still get it. Any help is welcome.
def validate
if org_name.blank?
errors.add(:org_name, :blank, :default => nil)
else
if (org = Organization.find_by_org_name(org_name)) && org != self
errors.add(:org_name, :taken, :default => nil, :value => org_name)
end
end
end
to
validates :org_name, :presence => true
validates_uniqueness_of :org_name, :scope => :org_id
Ths is the Rails 3 syntax for uniqueness validtion:
validates :org_name, uniqueness: {scope: :org_id}
This is easy to fix.
Firstly, analyse the error message:
Org name translation missing:
en.activerecord.errors.models.user.attributes.org_name.blank
This is caused by the following line of code:
errors.add(:org_name, :blank, :default => nil)
When you call the above, you are telling rails to look for a translation whose key is :blank. You probably didn't set that up yet, so to do that, just go into your locales file (config/locales/en.yml), and add the following:
en:
hello: "Hello world"
activerecord:
errors:
models:
organization:
attributes:
org_name:
blank: "can't be blank."
Hopefully that will fix it for you.
hiho
Is there any way to tell rails that my string may not be 'something'?
I am searching for something like
validates :string, :not => 'something'
thanks
klump
Either of these will do the job (click on the methods for documentation):
Probably the best and fastest way, easy to extend for other words:
validates_exclusion_of :string, :in => %w[something]
This has a benefit of using a regexp, so you can generalise easier:
validates_format_of :string, :without => /\A(something)\Z/
You can extend to other words with /\A(something|somethingelse|somemore)\Z/
This is the general case with which you can achieve any validation:
validate :cant_be_something
def cant_be_something
errors.add(:string, "can't be something") if self.string == "something"
end
To get exactly the syntax you proposed (validates :string, :not => "something") you can use this code (a warning though, I discovered this while reading the master branch of the rails source and it should work, but it doesn't work on my ~ 3 months old install). Add this somewhere in your path:
class NotValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << "must not be #{options{:with}}" if value == options[:with]
end
end
A couple of ways. If you have exact list of what it can't be:
validates_exclusion_of :string, :in => ["something", "something else"]
If you want to ensure that it doesn't exist as a substring at all:
validates_format_of :string, :with => /\A(?!something)\Z/
If it is more complicated and you want to hide the messy details:
validate :not_something
def not_something
errors.add(:string, "Can't be something") if string =~ /something/
end
What is the best/easy way to validate an email address in ruby (on the server side)?
You could look whether or not it matches a regexp like the one used in this Rails validator:
validates_format_of :email,:with => /\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z/
But if you use Devise, simply do:
validates_format_of :email,:with => Devise::email_regexp
Source: http://lindsaar.net/2008/4/14/tip-4-detecting-a-valid-email-address
Edit 1:
useful website for tests: http://www.rubular.com/
In Ruby? The same way as in any language.
Send a confirmation email to the address with a link that the recipient has to click before the email address is considered fully validated.
There are any number of reasons why a perfectly formatted address may still be invalid (no actual user at that address, blocked by spam filters, and so on). The only way to know for sure is a successfully completed end-to-end transaction of some description.
validates :email, presence: true, format: /\w+#\w+\.{1}[a-zA-Z]{2,}/
checks that email field is not blank and that one or more characters are both preceding the '#' and following it
Added specificity, any 1 or more word characters before an the #and any 1 or more word character after and in between specifically 1 . and at least 2 letters after
I know that this is a old question but I was looking for a simple to way to do this. I came across a email_validator gem this is really simple to set up and use.
as a validator
validates :my_email_attribute, :email => true
Validation outside a model
EmailValidator.valid?('narf#example.com') # boolean
I hope that this help everyone.
Happy Codding
Shortcut Form:
validates :email, :format => /#/
Normal Form (Regex) :
validates :email, :format => { :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ }
Source: Validator Class
You can use
<%=email_field_tag 'to[]','' ,:placeholder=>"Type an email address",:pattern=>"^([\w+-.%]+#[\w-.]+\.[A-Za-z]{2,4},*[\W]*)+$",:multiple => true%>
Since the main answer's blog site was down, here is the snippet of code from that site via nice cacher or gist:
# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
# Domain must be present and have two or more parts.
def validate_each(record, attribute, value)
address = Mail::Address.new value
record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
end
end
You can take reference from https://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_format_of
validates_format_of :email, with: /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
If you are using Rails/Devise - addition to #apneadiving`s answer -
validates_format_of :email,:with => Devise::email_regexp
Devise::email_regexp is taken from config/initializers/devise.rb
config.email_regexp = /\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z/
Send a confirmation mail , and I will usualy use this validator ... D.R.Y.
# lib/email_validator.rb
class EmailValidator < ActiveModel::EachValidator
EmailAddress = begin
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
quoted_pair = '\\x5c[\\x00-\\x7f]'
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
domain_ref = atom
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
word = "(?:#{atom}|#{quoted_string})"
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
local_part = "#{word}(?:\\x2e#{word})*"
addr_spec = "#{local_part}\\x40#{domain}"
pattern = /\A#{addr_spec}\z/
end
def validate_each(record, attribute, value)
unless value =~ EmailAddress
record.errors[attribute] << (options[:message] || "is not valid")
end
end
end
in your model
validates :email , :email => true
or
validates :email, :presence => true,
:length => {:minimum => 3, :maximum => 254},
:uniqueness => true,
:email => true