Newbie question regarding basic AJAX form verification


#1

Hi,

I’m confused about how form verification with AJAX works. Let’s say I
have a
‘new.rhtml’ view that contains a form with two fields, and the form
action is
‘create’. Let’s assume that I also want to do some type of data
validation on
one of the two fields via AJAX before I submit the form. But since
I’ve
already defined my form action with form_tag() as ‘create’, I can’t also
use
form_remote_tag() at the same time, right?

I’m sure I’m missing somethign obvious. but I could use some help.

Thanks,

Stan Mcfarland


#2

You could do your data validation directly in the method “create” of
your controller. If data are wrong, just redisplay your form with a
message error …

Thomas.


#3

Thomas <thomas_mahier@…> writes:

You could do your data validation directly in the method “create” of
your controller. If data are wrong, just redisplay your form with a
message error …

Thomas.

Yeah, but that’s not what I need to do. Think, for example, how a spell
check
application would work. You have one button to submit the form, and
another
button (or link) that does a spell check on a single textarea. This is
what I
can’t figure out how to do.

-stan


#4

Hi Stan,

I’d have a look at observe_field.

http://www.railsmanual.org/module/ActionView::Helpers::PrototypeHelper/observe_field

There is, as I’m often fond of pointing out, some stuff in Chad F.'s
Rails Recipes on this. The example is the on-the-fly search (a la
Google Suggest), but you can use observe_field to pass the contents of
an individual field to an action periodically (say once a second) and
return, say, some RJS to highlight the field red if it doesn’t validate.

observe_field obviously only works with one field at a time, so I’m not
sure how you’d pass the whole form periodically.

<fx: googles around />

Oh wait, observe_form…of course it would be called observe_form :slight_smile:

http://www.railsmanual.org/module/ActionView::Helpers::PrototypeHelper/observe_form

Alan


#5

On 2/17/06, Stan McFarland removed_email_address@domain.invalid wrote:

Yeah, but that’s not what I need to do. Think, for example, how a spell
check
application would work. You have one button to submit the form, and
another
button (or link) that does a spell check on a single textarea. This is
what I
can’t figure out how to do.

-stan

You can give the spell-check submit button a name, which you can then
test
for in @params within your action.

  • Rowan