Custom pre-validation

Hi. I’m trying to do a pre-validation in a form. What I need is to do
some validations, then, depending on this validation, ask a couple of
questions to the user and to process the form, including the user’s
answers.

For instance

Name: John
Last name: Doe
Country: Mars

I need to validate the existence of the country but just to give a
warning to the user. Something like

“The country is not on our database. Some features won’t be available
for you. Do you want to proceed?”
And a yes/no checkbox.

I did a custom validation in the controller and it works but, since I’m
not using the ActiveRecord validation way, the values of the form are
not retained.

Is there an ActiveRecord/Rails/Elegant way of doing this?

Thanks

I did a custom validation in the controller and it works but, since I’m
not using the ActiveRecord validation way, the values of the form are
not retained.

why don’t you use the custom validation?
in your view you could check with myrecord.errors.on(:country) if
there’s an error on country and show the checkbox if necessary

Is there an ActiveRecord/Rails/Elegant way of doing this?

yes. use the normal way
if that’s not possible, you can keep the form values by generating
a new record (without saving) and filling a form (maybe invisible)
with the values, so they’re handed back again later

Thorsten M. wrote:

in your view you could check with myrecord.errors.on(:country) if
there’s an error on country and show the checkbox if necessary

Maybe I’m wrong, but I think if I do it that way (using
myrecord.errors), the validation would prevent me from saving the
record. Usually that’s the expected behaviour but in my case I want the
validation just for a warning.

Thanks for your answer.

Greetings.

On 1/2/08, Fernando P. [email protected] wrote:

Country: Mars
not retained.

Is there an ActiveRecord/Rails/Elegant way of doing this?

Most of the validates_* methods accept an :if parameter where you can
place your conditional logic.


Greg D.
http://destiney.com/

On 1/3/08, Fernando P. [email protected] wrote:

Most of the validates_* methods accept an :if parameter where you can
place your conditional logic.

They do, but how can I include a checkbox as a validation’s response.

You have get your checkbox’s value into the model. I do this by
merging the object into the params:

params[:foo].merge( :foo => foo )

Then inside the model add an :if to the validation, something like this:

:if => Proc.new { |model| model.foo.checkbox_fieldname == 1 }


Greg D.
http://destiney.com/

Most of the validates_* methods accept an :if parameter where you can
place your conditional logic.

They do, but how can I include a checkbox as a validation’s response.
Currently, I’m doing

def validate

if self.country.not_exist?
errors.add(“Country does not exist. Do you want to proceed?” +
“OK<input id=‘contact_proceed’ name=‘contact[proceed]’
type=‘checkbox’” +
“value=‘1’ />”
end
end

Actually, it’s working but I don’t like at all this implementation so
I’m still hoping a better solution.

Thanks.