0

I want to skip the validations of few attributes during creation of a new user like address, pin, phone number etc.
However, it still need to do the other validations in the model when user tries to edit it. I tried using :on => :update but that doesn't help me. Any suggestions ?

My Code:

validates :address, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :city, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :state, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :zip, :presence => true, :numericality => true, :on => :update, :length => { :is => 5 }
2
  • Could you post some code that you have tried? That might help us figure out what the problem is. Commented Feb 24, 2012 at 17:43
  • validates :address, :presence => true, :length => { :maximum => 50 }, :on => :update validates :city, :presence => true, :length => { :maximum => 50 }, :on => :update validates :state, :presence => true, :length => { :maximum => 50 }, :on => :update validates :zip, :presence => true, :numericality => true, :on => :update, :length => { :is => 5 } Commented Feb 24, 2012 at 17:46

4 Answers 4

5

According the the documentation, what you need to do is something like this. Are you saying this is not working?

class Person < ActiveRecord::Base
  validates_presence_of :address, :on => :update
  validates_presence_of :pin,     :on => :update
end
Sign up to request clarification or add additional context in comments.

Comments

4

The validation process on save can be skipped by passing :validate => false.

Note that is there are database constraints you'll still get an error. e.g. if you use a rails migration and have :null => false when it is created (by running the migration) the actual database column will have that restriction be at the database level. A good thing as validations should be in both places. The way to override the db constrainst (i.e. you can't) would be a migration to actually remove the constraint.

3 Comments

@Michale Durrant. By passing false to the save will skip all my validations at creation. I still want to validate few fields during creation.
Ahh, yes 'a few' didn't jump out. Let me clarify the question for us, emphasizing that.
and to stop a lot more answers that missed it like below too.
2

On creation of record:

@model = Model.new(params[:model])
@model.save false

This will skip the validation.

2 Comments

But I still want to validate some attributes during creation.
stackoverflow.com/questions/3542819/… this thread will help you.
2
validates :address, :presence => true,
                      :length => { :maximum => 50 },
                      :if => :address_changed?

  validates :city, :presence => true,
                   :length => { :maximum => 50 },
                   :if => :city_changed?

  validates :state, :presence => true,
                    :length => { :maximum => 50 },
                    :if => :state_changed?

  validates :zip,   :presence => true,
                    :numericality => true,
                    :length => { :is => 5 },
                    :if => :zip_changed?

Adding if=> :attribute_changed? will solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.