Oct 20

Ruby Rails : How to bypass skip validation in Devise

Since my last post with email validations, I was looking for a solution to integrate it with devise

Sadly for me, devise implements the old way of validating email with regexp :

# From devise gem version 1.1.3
validates_format_of     :email, :with  => email_regexp, :allow_blank => true

Well, as I don’t want to hack activemodel’s code, I prefer to skip the validates_format_of method in User class.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  include SkipableMethod
  # Devise is great but uses the oldest way with regexp to validate email
  skip_and_restore_method(:validates_format_of) do
    devise :database_authenticatable, :registerable,
      :recoverable, :rememberable, :trackable, :validatable
  # Replace with a validation of my own
  validates :email, :email => true, :presence => true
  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

And then the library :

module SkipableMethod
  module ClassMethods
    def __skip_method(*args);end
    def skip_and_restore_method(*method_names,&block)
      method_names.each do |method_name|
        instance_eval <<-EVAL
        alias #{method_name}_skipped #{method_name}
        alias #{method_name} __skip_method
      method_names.each do |method_name|
        instance_eval <<-EVAL
        alias #{method_name} #{method_name}_skipped
        undef #{method_name}_skipped
  def self.included(receiver)
    receiver.extend         ClassMethods


Some tests :D


1 ping

Skip to comment form

  1. phaedryx

    Why not just remove the “:validatable” from your devise list and then write your own validations for the User model?

  2. Hallelujah

    Yes it can be a solution but I need to rewrite other validations …

    I just want to use my own email validation

  3. Naveed

    thanks it got solution for my problem from :allow_blank => true

  4. Andrew Sullivan Cant

    Thanks for writing this, it and valid_email let me do just what I want.

    I was wondering what license you intended this to be published with? I would like to use it in a project where I am required to be careful about licenses.

    Have you consider putting this into a gem?

  1. Email validation in Ruby On Rails 3 or Active model without regexp « La rolls des blogs

    [...] use this with devise, see my blogpost here : Ruby Rails : How to bypass skip validation in Devise class User < ActiveRecord::Base validates :email, :presence => true, :email => true [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>