Forum OpenACS Development: implementing view/add/edit page with ad_form

I have a situation where it's ok for all three pages to have the same basic layout, so it's appropriate to do this with ad_form. I've got it working except for one small detail. If my key value is set, meaning we're either viewing or editing an existing record, ad_form_new_p always returns true, meaning that I have no way (that I know of, anyway) to distinguish between the view and edit cases.

The documentation for ad_form_new_p say "It returns 1 if the current form being built for the entry of new data, 0 if for the editing of existing data.". This doesn't make a whole lot of sense to me. If you're entering new data, most of the time you won't have a pre-allocated value for the key field so you won't be able to call ad_form_new_p at all.

I found a thread where Joel gives an example of a view/edit use of ad_form and he seems to be able to use ad_form_new_p to distinguish between the two cases, so I don't know where I'm going wrong here.

This matters for several reasons - I want to be able to set the page title correctly, and more importantly I think the return value of ad_form_new_p determines whether the new_data or edit_data block is executed.

Any suggestions on the right way to do this? An example from the toolkit would be wonderful; I've looked around but it seems that every use of ad_form looks a bit different, and I don't see any so far that are trying to do quite the same thing I'm trying to do.

Thanks in advance!

Collapse
Posted by Don Baccus on
"This doesn't make a whole lot of sense to me. If you're entering new data, most of the time you won't have a pre-allocated value for the key field so you won't be able to call ad_form_new_p at all."

Huh? What makes you say that, the code does this:

set form [ns_getform]

    return [expr {[empty_string_p $form] || [ns_set find $form $key] == -1 || [ns_set get $form __ne
w_p] == 1 }]

It checks to see that you DON'T have a pre-allocated value, that's HOW it determines that it's a new form rather than edit form. The second check for __new_p has to do with the refreshing mode associated with javascript widgets IIRC (it's been over two years since I wrote it).

Are you sure you're passing the right key value, and that you declared it "optional" rather than give it a default value in ad_page_contract?

Collapse
Posted by Janine Ohmer on
"Huh? What makes you say that"

I was confusing passing the value with passing the name of the key. However, I was actually doing it correctly in my code, and it's still not working. The problem is that both the view and edit cases have the key set, so if that's all ad_form_new_p does to distinguish them it's never going to work for this.

Is there anything else in the form to distinguish the two?

Collapse
Posted by Don Baccus on
neither the "display" or "edit" case involve a new object, these modes either display or edit an EXISTING object.

ad_form_new_p does exactly what it says: it returns true if you're working with a from without a pre-existing object.

As to how to differentiate between the display and edit case, I don't know offhand. Lars added that capability and I was not consulted.

Collapse
Posted by Claudio Pasolini on
To distinguish between the view/edit modes, so as to set an appropriate page title, I normally set the default mode to 'edit' in the ad_page_contract and then check it:
if {[ad_form_new_p -key item_id]} {
    set page_title "Add a new project"
} else {
    if {[string equal $mode "edit"]} {
        set page_title "Edit project"
    } else {
        set page_title "View project"
    }
}
Remember to pass the mode parameter to ad_form and when calling the add/view/edit program to just view an item's data add the mode=display to its url.