Rails 3 - how to skip a validation rule?

I have this verification rule for the registration form:

validates :email, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :format => { :with => /\A[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.' }, :uniqueness => true 

This rule checks if the user fills in the email address of the registration form (+ its correct format).

Now I'm trying to add the ability to log in using Twitter. Twitter does not provide user email address.

How to skip the verification rule above in this case?

+4
source share
5 answers

You can skip checking when saving the user in code. Instead of user.save! use user.save(:validate => false) . Learned this trick from the Railscasts episode on Omniauth

+5
source

I'm not sure if my answer is right, just trying to help.

I think you can take advantage of this issue . If I change the accepted answer to your question, it will look like ( DISCLAIMER : I could not verify the following codes, since env is not ready on the computer I am currently working on)

 validates :email, :presence => {:message => 'cannot be blank.', :if => :email_required? }, :allow_blank => true, :format => { :with => /\A[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.' }, :uniqueness => true def email_required? #logic here end 

Are you updating the email_required? method email_required? to determine if he is from twitter or not! If from twitter, return false, otherwise true.

I believe you need to use the same :if for: validation validator. otherwise it will happen. Although, I'm also not sure :(. Sorry

+2
source

It seems you are doing two separate checks:

  • If the user provides an email address, confirm its format and uniqueness
  • Confirm your email address unless it’s registered on Twitter

I would do this as two separate checks:

 validates :email, :presence => {:message => "..."}, :if => Proc.new {|user| user.email.blank? && !user.is_twitter_signup?} validates :email, :email => true, # You could use your :format argument here :uniqueness => { :case_sensitive => false } :unless => Proc.new {|user| user.email.blank?} 

Additional Information: checking email format only if not empty Rails 3

+1
source

Skipping individual checks Skipping an individual check requires a bit more work. We need to create a property on our model called skip_activation_price_validation:

 class Product < ActiveRecord::Base attr_accessor :skip_activation_price_validation validates_numericality_of :activation_price, :greater_than => 0.0, unless: :skip_activation_price_validation end 

Then we set the true property anytime we want to skip checks. For instance:

 def create @product = Product.new(product_params) @product.skip_name_validation = true if @product.save redirect_to products_path, notice: "#{@product.name} has been created." else render 'new' end end def update @product = Product.find(params[:id]) @product.attributes = product_params @product.skip_price_validation = true if @product.save redirect_to products_path, notice: "The product \"#{@product.name}\" has been updated. " else render 'edit' end end 
0
source

The best way:

It will check email when the user is not following Twitter, and will also skip checking email when signing up with Twitter.

  validates :email, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :format => { :with => /\A[A-Za-z0-9._%+-] +@ [A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.' }, :uniqueness => true unless: Proc.new {|user| user.is_twitter_signup?} 
0
source

Source: https://habr.com/ru/post/1437037/


All Articles