Hello guys, I’m trying to validate the input of an integer from a form,
here is a bit of code:
This is the definition of the offending column in the migration:
t.column :position, :integer, :default => 0, :limit => 3, :null => false
And here is how I try to validate it inside de model:
validates_length_of :position, :within => 1…3
And this is the exception I get when I submit the form:
NoMethodError
private method `split’ called for 0:Fixnum
Maybe I’m not supposed to validate the length of integers at all?
I’m experimenting with Rails in these days, I came across another
problem:
In my model Provider I have the following validation:
validates_length_of :telephone, :in => 7…32, :allow_nil => true
What I try to do is:
- If you input anything it gets validated against the length, BUT you
could also enter nothing, and it would be ok. How do I accomplish this?
Thanks in advance for your help.
All integers are the same length 4 bytes. So no you don’t verify the
length of them. But you can validate that they fall within a
particular range of values if you want.
On Jul 24, 6:28 pm, Adrian De la cruz <rails-mailing-l…@andreas-
Robert W. wrote:
What is your question?
The validation you show should be correct. It should do exactly what
you want. If the field is left empty allow_nil => true should skip
validation of length.
On Jul 24, 6:57 pm, Adrian De la cruz <rails-mailing-l…@andreas-
Well, the problem is it doesn’t skip the validation. If I leave it blank
Rails complains with this message:
ActiveRecord::RecordInvalid
Validation failed: Telephone is too short (it should be 7 characters
minimum)
It seems as though you can only use validates_length_of with strings.
So you can either do what a previous poster mentioned, and use the
following:
this will ensure that the position value is between 1 and 3 characters
validates_length_of :position, :within => 1…999
or you can do something like this:
def validate
errors.add(:position, “should be between 1 and 3 characters in
length”) if position.to_s.length < 1 or position.to_s.length > 3
end
Adam
It seems as though you can only use validates_length_of with strings.
So you can either do what a previous poster mentioned, and use the
following:
this will ensure that the position value is between 1 and 3 characters
validates_length_of :position, :within => 1…999
What about negative values? -22 is 3 chars, but would fail on the
above…
? I admit I don’t know what hte original problem was…
What is your question?
The validation you show should be correct. It should do exactly what
you want. If the field is left empty allow_nil => true should skip
validation of length.
On Jul 24, 6:57 pm, Adrian De la cruz <rails-mailing-l…@andreas-
Thanks a lot for your help guys, as for the second problem I described,
I found the solution to be:
validates_length_of :telephone, :within => 8…24, :allow_nil => true,
:if => Proc.new{ | client | client.telephone != ‘’ }
What this validation does is: if something is entered in the form then
it gets validates, if nothing is entered in the form it gets skipped.
On 7/25/07, Philip H. [email protected] wrote:
It seems as though you can only use validates_length_of with strings.
So you can either do what a previous poster mentioned, and use the
following:
this will ensure that the position value is between 1 and 3 characters
validates_length_of :position, :within => 1…999
What about negative values? -22 is 3 chars, but would fail on the above…
? I admit I don’t know what hte original problem was…
yeah good point, it was also supposed to be validates_inclusion_of
instead of validates_length_of. You could use :within => -999…999 if
you wanted then, or use the manual validation with to_s (and take into
account any leading negative signs).
Adam
On 7/25/07, Adrian De la cruz [email protected] wrote:
Thanks a lot for your help guys, as for the second problem I described,
I found the solution to be:
validates_length_of :telephone, :within => 8…24, :allow_nil => true,
:if => Proc.new{ | client | client.telephone != ‘’ }
What this validation does is: if something is entered in the form then
it gets validates, if nothing is entered in the form it gets skipped.
if the telephone column definition in your database is set to default
to null, you shouldn’t need the :if => Proc.new … line. That’s what
:allow_nil => true is for. It doesn’t execute the validation if the
field was empty.
Adam
Adam C. wrote:
On 7/25/07, Adrian De la cruz [email protected] wrote:
Thanks a lot for your help guys, as for the second problem I described,
I found the solution to be:
validates_length_of :telephone, :within => 8…24, :allow_nil => true,
:if => Proc.new{ | client | client.telephone != ‘’ }
What this validation does is: if something is entered in the form then
it gets validates, if nothing is entered in the form it gets skipped.
if the telephone column definition in your database is set to default
to null, you shouldn’t need the :if => Proc.new … line. That’s what
:allow_nil => true is for. It doesn’t execute the validation if the
field was empty.
Adam
Well, that was exactly why I posted my question in the first place, I
did too expect the application to behave that way, but it doesn’t.
Here is the column definition for the telephone field:
t.column :telephone, :string, :limit => 24, :null => true
If you only validates like this in your model, it won’t work as
expected:
validates_length_of :telephone, :within => 8…24, :allow_nil => true
After further investigation I realized that the problem is the form,
isn’t sending a nil value back, but a blank one e.g. ‘’. And that’s the
problem.
I even read about a patch regarding this problem, it suggest that the
validates_length_of method should be able to take an option called:
“:allow_blank” that does what the Proc.new code does right now… but it
seems it hasn’t been commited yet
Yeah, thanks for all the help you provided.
As for integers, the validates_inclusion_of does what I wanted to do
with integers, and better, because you can specify the exact range to
cover.
Regards,
Adrian.
On 7/25/07, Adrian De la cruz [email protected] wrote:
What this validation does is: if something is entered in the form then
did too expect the application to behave that way, but it doesn’t.
I should clarify: using :allow_nil => true won’t work with a string
attribute, only with an integer type attribute. So using :allow_nil
=> true would’ve worked for your initial question where you were
trying to validate the length of an integer (position column),
however, the problem in that instance was that validates_length_of
cannot be used with an integer. And as for the problem with the
telephone number validation, since that’s a string, you can’t use
:allow_nil => true with it.
So looks like you’re back to your solution of using the :if condition
with string type attributes, at least until that patch is committed…
Adam
Adam C. wrote:
cannot be used with an integer. And as for the problem with the
telephone number validation, since that’s a string, you can’t use
:allow_nil => true with it.
Just to clear up things, yes you can use :allow_nil => true with
strings, it allows to do stuff like this in your tests:
p = Provider.create( :name=>“John”, :nickname=>“hammer” )
If you didn’t have :allow_nil => true, or have set it to false, the line
of code above would have triggered a validation error, and you would be
forced to do something like this instead:
p = Provider.create( :name=>“John”, :nickname=>“hammer”, :telephone =>
‘’ )
Regards,
Adrian.