Validates numericality issue

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.

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

Colin Law wrote:

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

Colin Law 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.

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

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.

pepe wrote:

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.

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

Thanks

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 (ActiveRecord::Callbacks).

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. :wink:

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.