Forum OpenACS Q&A: Behaviour of ad_form's {value "..."} form element parameter

I am trying to create a form with a text field that has the following (presumably common) features:

- Contains an initial 'default' value, and
- Is required (ie, not blank)

Unless i'm missing something obvious, this doesn't appear easily possible using ad_form. If i include a {value "foo"} parameter for the form element, clear the "foo" text and submit the form with that element blank, the -on_submit block still gets "foo" as the value (presumably by design?).

A workaround is use a -validate block to test the new value against the original, and if they match then the user either didn't change the field, or deleted it. But you still can't tell which.

It'd be nice if we had two new form element parameters:

{initial_value ...} The value of the element when the form is first created, and
{default_value ...} The value used if the field is blank

with the existing {value ...}'s behaviour unchanged for backwards compatibility, and documented as 'both' of the above.

Why don't you set foo in the page contract or in the new_request block like this:

ad_form -form {
foo:text
} -new_request {
set foo "bar"
}

I'm not using a key in my form so -new_request doesn't work.

ad_page_contract {} {
    {foo "bar"}
}

doesn't fill the form field, unless i also include a {value $foo}, in which
case an empty string is valid. I could however manually check for that - this works:

ad_page_contract {} {
    {foo "bar"}
}

ad_form -form {
    {foo:text {value $foo}}
} -validate {
    {foo {![string equal $foo ""]} "foo is a required field"}
} ...

You can use the -on_request block if you do now have a key in your form.