Forum OpenACS Q&A: setting __refreshing_p to 1?

Collapse
Posted by James Thornton on
I can't seem to set __refreshing_p to true -- what's the proper way to do this with ad_form?

Also, if it's set to true, and I use {html {onchange submit()}} for the widget, the form will refresh, execute the code in -on_refresh, and set any values in the refreshed form that I have specified in -on_refresh -- right? Is that the proper way to use -on_refresh?

Collapse
Posted by Jerry Asher on
I am working on a page who's author uses __refreshing_p.

What they have is a li'l piece of javascript attached to the form element

{html {on change "document.info_form.__refreshing_p.value='1'; document.info_form.refresh_p.value='1';document.info_form.submit()"}}
They also have a hidden form element refresh_p
{refresh_p:text(hidden) {label "Refresh"} {value 0}}
They check refresh_p in validate blocks. If it's set they "excuse" blocks that don't validate.

In the -on_refresh block, they set the values of those calculated widgets values that uh, make sense to refresh. At the end of the form, they call

template::element set_value info_form refresh_p 0
Collapse
Posted by Deds Castillo on
I think I can remember that code.  During that time, I experimented with different combinations to make it work and that's the only answer I came up with.  Would be nice if someone could come up with a more "elegant" solution that can be used as future reference.
Collapse
Posted by Jerry Asher on
Hey Deds, I was really pleased to see how you got that to work!

Bye the way, to gain insight into how ad_form and the form template engine works, place something like this at the top of your .tcl page.

ns_log notice it's my page!
set mypage [ns_getform]
if {[string equal "" $mypage]} {
    ns_log notice no form was submitted on my page
} else {
    ns_log notice the following form was submitted on my page
    ns_set print $mypage
}
This print will all the crazy things POSTed to your page into your server log.
Collapse
Posted by Jade Rubick on
Great Great Great idea Jerry. I'm putting that on my ad_form page.
Collapse
Posted by James Thornton on
Rather than using refresh_p, why not simply make use of __refreshing_p...

} -validate {

{sku {$__refreshing_p || 0 < [string length [string trim $sku]]} "\"SKU\" must contain a value"}

}

Collapse
Posted by Randy O'Meara on
Jerry,

Could you post a more complete code fragment so I can understand what you all are talking about. It's sounds very interesting, I just do not comprehend... yet.

Randy

Collapse
Posted by Jerry Asher on
Hi Randy,

I don't know if I can or not....  In terms of copyright and confidentiality....  If James gets his page going, perhaps he can show a small demo of how that works.

Collapse
Posted by James Thornton on
Here is an abridged demo of vehicle.tcl..

set sql "select category, category_id from moto_category where parent_id IS NULL"
set category_options {"Select a Category" ""}
set category_options [linsert [db_list_of_lists get_vehicle_categories $sql] 0 $category_options]

if {[info exists __refreshing_p] && [string equal $__refreshing_p "1"]} {

    set sql "select category, category_id from moto_category where parent_id = :category_id"
    set subcategory_options {"Select a Subcategory" ""}
    set subcategory_options [linsert [db_list_of_lists get_vehicle_subcategories $sql] 0 $subcategory_options]

} else {
    set subcategory_options "-------------"
}

ad_form -name vehicle -form {

    vehicle_id:key(moto_vehicle_id_seq)

    {sku:text(text),optional {label "SKU"} {html {size 30}}}
    {category_id:integer(select),optional {label "Category"} {options {$category_options}}
      {html {onchange "document.vehicle.__refreshing_p.value='1';document.vehicle.submit()"}}}
    {subcategory_id:integer(select),optional {label "Subcategory"} {options {$subcategory_options}}}

# bunch of vars snipped

} -on_refresh {
    # didn't use -on_refresh
    # set subcategory_options before ad_form
    # because I couldn't find a way to set them here

} -validate {

    {sku {$__refreshing_p || 0 < [string length [string trim $sku]]} "\"SKU\" must contain a value"}
    {category_id {$__refreshing_p || 0 <[string length $category_id]} "\"Category\" must contain a value"}
    {subcategory_id {$__refreshing_p || 0 < [string length $subcategory_id]} "\"Subcategory\" must contain a value"}

# all vars are validated in the same way...

}

Collapse
Posted by Randy O'Meara on
Thank you, James and Jerry. I can see clearly now.