Validates numericality issue

20 views
Skip to first unread message

Ithelp Eighty-five

unread,
Aug 30, 2010, 1:01:10 PM8/30/10
to rubyonra...@googlegroups.com
Hello everyone I have a problem in my app with the validation on
numericality. The field should be all numbers, and if an user types for
example "12345abc" into the field an error is raised and the data is not
saved. But in the field where the error was raised it now says "12345",
I want this field to be blank instead.

What is the best way to go about doing this?


Thanks in advance for any advice offered.
--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Aug 30, 2010, 3:57:08 PM8/30/10
to rubyonra...@googlegroups.com
On 30 August 2010 18:01, Ithelp Eighty-five <li...@ruby-forum.com> wrote:
> Hello everyone I have a problem in my app with the validation on
> numericality. The field should be all numbers, and if an user types for
> example "12345abc" into the field an error is raised and the data is not
> saved. But in the field where the error was raised it now says "12345",
> I want this field to be blank instead.

In the update and/or create action, after the failed save, just blank
the field in the object being passed to the view. So if the object is
@my_thing and the field is value then set @my_thing.value = "" or
possibly nil might be better.

Colin

Ithelp Eighty-five

unread,
Aug 30, 2010, 4:17:41 PM8/30/10
to rubyonra...@googlegroups.com

Where should this code go? In the Model, View, or controller?

Colin Law

unread,
Aug 30, 2010, 4:53:14 PM8/30/10
to rubyonra...@googlegroups.com

After the failed save, which is in the update and/or create action of
the controller. I think maybe the fact that you had to ask that
question means you might benefit from working through the Rails Guides
(google it).

Colin

Ithelp Eighty-five

unread,
Aug 30, 2010, 4:59:34 PM8/30/10
to rubyonra...@googlegroups.com

Thanks I had just realize it went in the controller.

Another question... if I want to set a field blank for each field that
had an error how would i do that?

Srikanth Jeeva

unread,
Aug 31, 2010, 2:18:35 AM8/31/10
to rubyonra...@googlegroups.com

> Another question... if I want to set a field blank for each field that
> had an error how would i do that?

you can do this in view file..

give a sample that you use in ur view file. so that i could help it out.

--
sri

Ithelp Eighty-five

unread,
Aug 31, 2010, 10:10:07 AM8/31/10
to rubyonra...@googlegroups.com
Srikanth Jeeva wrote:
>
>> Another question... if I want to set a field blank for each field that
>> had an error how would i do that?
>
> you can do this in view file..
>
> give a sample that you use in ur view file. so that i could help it out.
>
> --
> sri

I want so that if an error is rasied all the fields where the errors is
raised will be cleared out(blank)

I did this in the controller by:
@company.name = nil

----------------------
View:

<span>First Name</span>
<%= @company.name.capitalize %>

<span>First Name</span>
<%= @company.scatid.capitalize %>

pepe

unread,
Sep 1, 2010, 9:04:24 AM9/1/10
to Ruby on Rails: Talk
> Another question... if I want to set a field blank for each field that
> had an error how would i do that?

I would do this in the model, after validations. I would use 'each' or
'valid?', depending on how you want to handle it.

If you clear the field in the model, after validations have run (and
you have your errors for display in the view), then your fields will
be already set for your view. I think that clearing the fields in the
controller or the view is a mistake as it's not their job to manage
the contents of the model values. I would only do this in the
controller if the default behavior is not to have the values cleared;
for example, if the values should be cleared in one part of your
application and not in another part of your application.

Ithelp Eighty-five

unread,
Sep 1, 2010, 5:58:00 PM9/1/10
to rubyonra...@googlegroups.com

Can you go into more detail on how to do this? I'm new to Ruby on Rails.

Thanks

pepe

unread,
Sep 2, 2010, 9:54:50 AM9/2/10
to Ruby on Rails: Talk
> Can you go into more detail on how to do this? I'm new to Ruby on Rails.

> > I would do this in the model, after validations. I would use 'each' or
> > 'valid?', depending on how you want to handle it.

Saving a record goes through a series of steps in Rails and at each
one of them you can write code in what is called a callback method
(http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html).

Before Rails tries to save a record validations are run. Those
validations might generate 'errors' (they are not really Ruby errors
since you don't need to "rescue" them, just errors of your model),
which are stored in an object so you can use them in your view.

'each' and 'valid?' are methods that allow you to work with those
errors.

'each' (http://api.rubyonrails.org/classes/ActiveModel/
Errors.html#method-i-each) will allow you to loop through the list of
errors and use that information to clear all fields with errors.

'valid?' (http://api.rubyonrails.org/classes/ActiveRecord/
Validations.html#method-i-valid%3F) allows you to check for a specific
field 'manually'.

The benefit of using 'each' is that it will just do everything by
itself once you have it set up, even if you add/remove columns to your
table. It's just a bit more complicated than using 'valid?' but it
will save you tons of time if you ever change the columns of the table
because you don't have to go back and look in the code to see why the
new fields are not been cleared and "where the heck did I put that
code that clears the fields anyway?", although if you do things "the
right way" this should be a mute point.

You can use the 'after_validation' callback to put your code in a
method:

<code>
after_validation :clear_fields

def clear_fields
# you clear your fields here
end
</code>

>
> > If you clear the field in the model, after validations have run (and
> > you have your errors for display in the view), then your fields will
> > be already set for your view.

If you do it as I mentioned above, in the model, your validations run
and generate errors (or not). If errors are generated already you have
them so you don't have to worry about 'losing' them so after the
callback code runs the errors will still be available.

>> I think that clearing the fields in the
> > controller or the view is a mistake as it's not their job to manage
> > the contents of the model values.

This is a separation of concerns issue. The controller should 'know'
very little about the model. The model should know what to do when
certain things happen to it. Think about it this way and if you have
children you will appreciate better, I think. Wouldn't it be nice that
you didn't have to worry about repeating for the 1,000,000th time to
your X year old kid to go to the bathroom instead of just doing it in
the diaper? Well, by the time that kid is a teenager (hopefully
sooner) he/she doesn't use a diaper anymore. Why? Because you taught
him/her to take care of him/herself. What if you "teach" your models
to take care of themselves? It's not the responsibility of the
controller to babysit the model, the model is an 'adult' and should
know how to take care of itself. The added benefit to this is that
your models are more 'receptive' to your teachings than your kids. ;)

>>I would only do this in the
> > controller if the default behavior is not to have the values cleared;
> > for example, if the values should be cleared in one part of your
> > application and not in another part of your application.

I think this is covered in the prior paragraph.
Reply all
Reply to author
Forward
0 new messages