# Custom pre-validation

**URL:** <https://www.ruby-forum.com/t/custom-pre-validation/124786>\
**Category:** Rails\
**Created:** [January 2, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786 "2008-01-02T17:27:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 2, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/1 "2008-01-02T17:27:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 2, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/2 "2008-01-02T18:05:21Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 2, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/3 "2008-01-02T19:13:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 2, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/4 "2008-01-02T23:01:10Z")

</div>

On 1/2/08, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 3, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/5 "2008-01-03T17:58:52Z")

</div>

On 1/3/08, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [January 3, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/custom-pre-validation/124786/6 "2008-01-03T14:02:06Z")

</div>

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