Forum OpenACS Q&A: scope of variables when using ad_form

Hi,
A simple ad_form question which is hopefully trivial to you gurus out there. Is there a way to set a variable before (or during) -validate that will make it available to -on_submit and other clauses? Is my case, I'm running an expensive validation proc in my -validate; I want to result of the proc to be available later on, without having to run the proc again.

Is it possible and can you give a quick example?

thanks
Brian

Collapse
Posted by Dave Bauer on
You can do this:
1) do the validation in the on_submit block
2) set form error on the element in question if it doesn't validate
3) return the template early and terminate the submit processing.

ie:

-on_submit {
if {![expensive_validation_proc]} {
element set_error $form_id $element_id "Error Message"
ad_return_template
ad_script_abort; # make sure you abort or the processing will continue
}

Collapse
Posted by Brian Fenton on
Thanks Dave,

that sounds good. By doing it that way, you don't "lose" any of the regular -validate functionality do you? I can't see how (as all -validate seems to do is set the error) but just making doubly sure (or as we say in Ireland "to be sure to be sure").

thanks as always
Brian

Collapse
Posted by Dave Bauer on
One trick, that I am not sure about if other elements do not validate, the on_submit block won't be processed and you will not be able to validate the element that uses the expensive proc unless everything else is validated.
Collapse
Posted by Brian Fenton on
Cool, good to know.

thanks for that.
Brian