Call specific model validators from controllers

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

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

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

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