Jay
Jay

Reputation: 6244

How can I bypass validates_email_format_of validation to save nil email value?

In multiple places in a Rails 4 app, the validates_email_format_of gem is the perfect solution. But when an email address bounces, I'd like to save a nil value to the email field.

In my Gemfile:

gem 'validates_email_format_of', git: 'https://github.com/alexdunae/validates_email_format_of.git'

In my User model:

validates_email_format_of :email, :message => 'Email format looks invalid. Did you mistype?'

In my view:

<%= button_to 'Delete this Email Address', delete_email_addy_admin_path(user.id), data: { confirm: 'Are you sure?'}, class: "btn_danger" %>

In the controller:

def delete_email_addy
   @user = User.find(params[:id])
   @user.email = nil
   @user.save!
   redirect_to email_bounces_admins_path
end

The result is that my message 'Email format looks invalid...' is produced. Using update_attributes instead of save or save! results in a rollback to the previous value.

Upvotes: 1

Views: 343

Answers (3)

rmlockerd
rmlockerd

Reputation: 4126

According to the gem documentation, it supports both :allow_nil and :allow_blank options. So:

validates_email_format_of :email, 
                          :message => 'Email format looks invalid. Did you mistype?',
                          :allow_nil => true

EDITED to answer your follow-up

If you want to sometimes allow nil and sometimes not, it also supports :if and :unless options. You don't need the model to know anything about the controller (nor should you). You can just add a non-persistent boolean attribute to the Model and then use :if/:unless to check it.

model

class YourModel < ActiveRecord::Base
  attr_accessor :skip_email_validation

  validates_email_format_of :email, 
                            message: 'Email format looks invalid.',
                            unless: -> { skip_email_validation? }
  ...
end

controller

def delete_email_addy
   @user = User.find(params[:id])
   @user.email = nil
   @user.skip_email_validation = true
   @user.save!
   redirect_to email_bounces_admins_path
end

A quick look at @Karl's gem indicates that it automates the process of adding those non-persistent attributes. If you've got lots of cases where you need to conditionally skip validation and/or don't mind an additional gem, super, but for a single case it is probably just as easy to roll your own.

Upvotes: 4

Karl Wilbur
Karl Wilbur

Reputation: 6187

Using allow_nil: true is a simple handling for allowing nils but I have a validation_skipper gem that could handle more complex needs.

From my Gemfile:

# Rails gem that lets you skip validations
gem 'validation_skipper', '>= 1.3.0', github: 'K-and-R/validation_skipper'

Using this gem, you can have more complex logic around exactly when to skip validation.

Upvotes: 1

Ben Trewern
Ben Trewern

Reputation: 1740

It looks like you can use:

validates_email_format_of :email,
    :message => 'Email format looks invalid. Did you mistype?',
    :allow_nil => true

Upvotes: 2

Related Questions