Validates_length_of not working with :if?

Hello,

I’m trying to cut down the errors if the guy already getting the empty
nickname, so he doesn’t get anything about the nickname is short.

validates_presence_of :nickname

This below is not working ?

validates_length_of :nickname, :within => 4…40, :if => Proc.new {
|user| user.nickname.length > 1 }

nor this one below?

validates_length_of :nickname, :within => 4…40, :if => nickname.length

1

Anyone have a solution for this?

Thanks :slight_smile:

validates_presence_of :nickname
validates_length_of :nickname, :in => 4…40, :allow_nil => true

but just make sure that you change the nickname param from “” [empty
string]
to nil or it won’t work. “”.nil? => false. I dunno why there’s not a
:allow_blank => true which could cover nil? and empty?

RSL

Russell N. wrote:

validates_presence_of :nickname
validates_length_of :nickname, :in => 4…40, :allow_nil => true

but just make sure that you change the nickname param from “” [empty
string]
to nil or it won’t work. “”.nil? => false. I dunno why there’s not a
:allow_blank => true which could cover nil? and empty?

RSL

Hmm, thats kind of weird, in the Web D. with Rails the book
give a example to this.

validates_length_of :nickname, :in => 4…40, :if => Proc.new {|u|
u.nickname.blank?

But this are not working?

Your example are not working either, it gives me “undefined local
variable or method ‘allow_nil’” ?

Russell N. wrote:

Perhaps you have other errors? I dunno how else to help. Here’s the API
page
for validates_length_of which clearly states that :allow_nil is a valid
parameter.

ActiveRecord::Validations::ClassMethods

Sigh.

RSL

Yeah, I’m wondering about that too!

Kind of weird :frowning:

But thanks for your help :smiley:

Perhaps you have other errors? I dunno how else to help. Here’s the API
page
for validates_length_of which clearly states that :allow_nil is a valid
parameter.

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000942

Sigh.

RSL