Forum: Ruby on Rails AJAX calls don't do validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
SteveC (Guest)
on 2006-02-13 01:30
(Received via mailing list)
Chatting on the IRC channel today to people (thanks!) it came up that
submitting a form remote bypasses the :validates... in the model. At
least thats how it looks from my tests and what people said is the
current behaviour.

Is this the case?

If so, is there a way around it or will it change in the future? I can
imagine good reasons for the current behaviour.

have fun,

SteveC removed_email_address@domain.invalid http://www.asklater.com/steve/
Rick B. (Guest)
on 2006-02-13 02:24
(Received via mailing list)
* SteveC (removed_email_address@domain.invalid) [060212 18:33]:
> Chatting on the IRC channel today to people (thanks!) it came up that
> submitting a form remote bypasses the :validates... in the model. At
> least thats how it looks from my tests and what people said is the
> current behaviour.
>
> Is this the case?

I don't believe this to be the case.  The form_remote routine is
invoking a controller method which by default isn't going to know or
care how it's called.

When the controller manipulates models it calls the Ruby methods defined
in the model classes.  From the model's point of view a call from a
controller called by form_remote looks identical to a call from a
controller not called by form_remote, which looks identical to a call
not coming from a controller at all (e.g., by Ruby code not using
anything other than the AR part of Rails, or even code called from
script/console).  If model methods are called which trigger validation
in normal circumstances, they should trigger validation in the
circumstances you describe.

It seems to me it would be cumbersome to implement the "don't call
validators when using form_remote" semantics even if that's what one
wanted, and I can't imagine that's what the Rails developers have
wanted.

That being all theoretical, in practice we're using AJAX heavily,
including lots of form_remote calls, and relying on the validators
in the models quite heavily.  They do indeed get called, at least in our
experience.

Perhaps you mean something else?

Rick
--
 http://www.rickbradley.com    MUPRN: 307
                       |  website as in
   random email haiku  |  a regular (but high
                       |  quality) e-shop.
Henrik N (Guest)
on 2006-02-13 23:14
SteveC wrote:
> Chatting on the IRC channel today to people (thanks!) it came up that
> submitting a form remote bypasses the :validates... in the model. At
> least thats how it looks from my tests and what people said is the
> current behaviour.
>
> Is this the case?
>
> If so, is there a way around it or will it change in the future? I can
> imagine good reasons for the current behaviour.
>
> have fun,
>
> SteveC removed_email_address@domain.invalid http://www.asklater.com/steve/

Not sure if I understand you correctly, but I wrote some code to get
validations with AJAX:
http://wiki.rubyonrails.org/rails/pages/HowToValid...

I'm pretty sure e.g. link_to_remote does not bypass validations, but
since the errors occur within the javascript-triggered connection, you
only get to see the errors if you pass them back somehow (as exemplified
on the page I linked above).
This topic is locked and can not be replied to.