Forum OpenACS Q&A: ad_form: What's my mode?

Collapse
Posted by Sean Redmond on

So ad_form_new_p will tell you if you are editing existing data or adding a new record, but is there something that will tell you if you are in display mode or edit mode? In the rendered form I see <input type="hidden" name="form:mode" value="display" /> vs. <input type="hidden" name="form:mode" value="edit" /> but I can't figure out how to access it.

I have drawings with dimensions, and I want to test the mode so that I can have the dimensions show up for "display" as one item, e.g., "11 x 17" but for "edit" have one row for height and one row for width.

Collapse
Posted by Dave Bauer on
Sean,

In the ad_form declaration should be a -mode switch that has the value of display or edit. If -mode is not specified the form will always be in edit mode.

Collapse
Posted by Sean Redmond on

Dave,

That's how you set it, and I'm setting it to "display", but how do you tell that the user has clicked the "Edit" button and that you are now actually in edit mode?

Collapse
Posted by Guan Yang on
You can just set a variable, like this:

ad_form ... \
    -new_request {
        set form_mode "new"
        ...
    } \
    -edit_request {
        set form_mode "edit"
        ...
    }
Collapse
Posted by Sean Redmond on

-edit_request comes to late, or the scope is wrong. Also, I'm not testing for new vs. edit but for display vs.edit. What I have to do is something like this:

ad_form -name myform -form {
    ...
}

set my_current_mode [ad_form_whats_my_mode -id myform]

if { $my_current_mode == "display" } {
    ad_form -extend -name myform -form {
        # Set up the form to display coumpound
        # values as a single, nicely formatted item
    }
} else {
    ad_form -extend -name myform -form {
        # Set up the form to display the same
        # items individually, each to its own row
    }
}

# Add anything else and set the action blocks
ad_form -extend -name myform -form {
    ...
} 

According to the docs "Any variables set in an -edit_request block are available to the ADP template as well as the form." But I need something that is available outside the form as you're building the form, but before you get to the ADP template. There should be an [add_form_editing_p] corresponding to [ad_form_new_p] but, since there isn't, there must be another technique.

Collapse
Posted by Sean Redmond on

This seems to work. You want to know either that the user clicked the edit button and therefore that you are probably in edit mode, or that there were errors and you are probably back in edit mode.

ad_proc ad_form_edit_p { id } {
    return \
    [expr [string equal [template::form::get_button $id] edit] \
        || [llength [template::form::get_errors $id]] ]
}

This probably won't work if you've set your own -actions