[html-formfu] FormFu + Ajax

Carl Franks fireartist at gmail.com
Thu Nov 27 13:45:10 GMT 2008


2008/11/27 Roberto Baglioni <baglioni at arcetri.astro.it>:
> Hi,
> I'm starting using FormFu and I have found a problem I don't know how to
> handle.
>
> I use Ajax to populate a Select field. When the user submits the
> form, FormFu checks the input, and if it finds some errors the form is
> displayed again for further input. The problem is that in this operation
> the items in the list, populated by Ajax and possibly the value selected
> by the user, got lost.
> Is there any mean to notify FormFu that the form is changed, or do you
> suggest
> other means to overcome the problem?

Hi Roberto,

There's 2 problems here.
The first is that because the Select's list of options isn't sent by
the browser, FormFu thinks the select has no option list.
The other is that when FormFu is re-rendering the form, because the
Select field has no list of options, nothing is marked as "selected",
so the submitted value is lost.

I can think of 2 ways around the first problem.

When the page is redisplayed after errors, I would use AJAX again to
re-populate the Select field's list of options - the same way you did
initially.
You'll also need to add a Hidden field, with a CopyValue Filter on it,
so that when the Select value is submitted to FormFu, that value also
gets copied to the hidden field.
Then, when the page is redisplayed after errors, use JavaScript to
check for the presence of that hidden field's value, and use it to set
the "selected" value of the select field.

The alternative would be when you're first populating the Select list
with AJAX, also stuff the list into a hidden field (maybe as
serialized JSON), so it will be sent back to FormFu.
Then use server-side code to parse that hidden field's value, and
recreate the Select field's option list.
Then, when you're redisplaying the form, the Select field should
automatically get the correct "selected" item set.

Hope this helps,
Carl



More information about the HTML-FormFu mailing list