# Call specific model validators from controllers

**URL:** <https://www.ruby-forum.com/t/call-specific-model-validators-from-controllers/91088>\
**Category:** Rails\
**Created:** [April 4, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/call-specific-model-validators-from-controllers/91088 "2007-04-04T16:35:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RJRJRJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8491ac/32.png) [@RJRJRJ](https://www.ruby-forum.com/u/RJRJRJ)\
**Post date:** [April 4, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/call-specific-model-validators-from-controllers/91088/1 "2007-04-04T16:35:52Z")

</div>

Here is the scenario.

Standard web form that maps directly onto a model (Lets say “user”  
model).  
I want to add ajax validation so that when the user exits each field a  
little request is fired off, the field is validated and if it doesn’t  
then the error message is displayed above the field.

I don’t want to duplicate the validations setup in te model. What I want  
is to validate the field against the respective validates\_ rules in the  
model that may or may not exist.

Can you call specific validations for a model? What I would envisage is  
something like

controller:

def validate

# returns error message if validation fails

result = User.validate\_the\_field(params[:field], params[:value])  
render the result etc etc  
end

Has anyone done this?

Gracias

Jeff

---

<div class="post-metadata">

**Author:** ![RJRJRJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8491ac/32.png) [@RJRJRJ](https://www.ruby-forum.com/u/RJRJRJ)\
**Post date:** [April 4, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/call-specific-model-validators-from-controllers/91088/2 "2007-04-04T16:59:22Z")

</div>

Jeff,

I haven’t done this but it doesn’t sound too hard.

1. Write a js snippet that fires off a call to a a custom controller  
action - let’s call it check\_user\_attribute().
2. Wire this snippet up with the onblur event handler of form’s fields.
3. In check\_user\_attribute, build user and check valid?, then check  
errors on ‘fieldname’. Update field as needed depending on outcome  
via rjs.

Note that this is resource-intensive with all the remote calls and  
running the full validation every time a user changes fields.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![RJRJRJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8491ac/32.png) [@RJRJRJ](https://www.ruby-forum.com/u/RJRJRJ)\
**Post date:** [April 4, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/call-specific-model-validators-from-controllers/91088/3 "2007-04-04T17:04:25Z")

</div>

> Note that this is resource-intensive with all the remote calls and  
> running the full validation every time a user changes fields.
> 
> –  
> Zack C.  
> [http://depixelate.com](http://depixelate.com)

That was my original plan but as yous said it is rather intensive and I  
don’t like the idea of creating a new instance of a model and running  
the full validation to just to check one field, I was hoping someone  
much cleverer than me had writtena plugin or gem that does the  
check\_user\_attribute stuff directly since this doesn’t seem like a  
totally unique case.

I am still not liking the idea of creating a User.new, maybe I’ll be  
crazy and delve into the validators themselves and see how they tick.

RJ
