AnApprentice
AnApprentice

Reputation: 110960

How to use Rails validates only if not a new user

I have the following in my user.rb:

validates :fname, :length => { :minimum => 1, :maximum => 100 }
validates :lname, :length => { :minimum => 1, :maximum => 100 }

How can I update this validations to only apply to existing users? I ask as I want to allow a user to signup without having to enter a fname or lname.

Ideas?

Upvotes: 2

Views: 2524

Answers (2)

sevenseacat
sevenseacat

Reputation: 25029

You can also use :on => :update, to make the validation only apply when a record is updated (as opposed to :on => :create)

Upvotes: 3

Ryan Bigg
Ryan Bigg

Reputation: 107728

validates :fname, :length => { :minimum => 1, :maximum => 100 }, :unless => :new_record?
validates :fname, :length => { :minimum => 1, :maximum => 100 }, :unless => :new_record?

Upvotes: 6

Related Questions