Add forming before saving and validation without encumbering "validate?"

I have a before_validation callback that reformats one of the entries. I need it to run before validation, to be sure, that formatted output was parsed correctly. But as far as I understand each time I call valid? to check if data is ok, the entry will be reformatted. Can I do this another way, because on valid? I just want to check if all the fields are valid and not change something.

Answers


this looks really similar to this one: Rails model validation on create and update only

You can do

before_validation(:on => :create) do
  reformat #method call, not a symbol
end
before_validation(:on => :update) do
  reformat #method call, not a symbol
end

but it still runs for me when I call valid?. Not sure why though, because that's not in the context of creating or updating, so I feel like Rails lies to us in this case.

If you could make it into a before_save, then it should work, since that runs after validation. You said you need to validate that it's still ok when you reformat, so maybe add a new method to validate the reformatting, and do

before_save :reformat, :validate_reformatting

or add the sanity check at the end of your method that you use to reformat.


Need Your Help

Creating a JTable from multiple Array lists

java arrays swing arraylist jtable

In a class called database; there are three String ArrayLists named firstName, surname and gender. The elements in the ArrayLists are parallel. For example surname(1) would be the surname of firstN...

How to export a project from IBM Bluemix PaaS to anywhere else as a Docker?

mobile web docker export ibm-cloud

I lead a web/mobile project and I still need to know the tools we will be using for development.