Forum OpenACS Q&A: Re: E-commerce 'category-browse-subcategory.tcl'

Collapse
Posted by Richard Hamilton on
# TCL PAGE BEGIN - remove me

# Demo of ad_form
ad_page_contract {
    @author Richard Hamilton (mailto:richard.hamilton@webteamworks.com)
    @creation-date 21-12-2008
    @cvs-id@ $Id$
    Demonstration of ad_form calls and multiple forms per page
} { 
} -properties {
    context:onevalue
}

set title [ad_quotehtml "Ricky's little ad_form demo"]
set context [list "Demo"]

# The following condition would tel you if the request is for a new form or if the form already exists - i.e. the
# user wants to edit the data already submitted. This is used when implementing an add/edit page.
#
#if {[template::form is_request form_one] } { }
#
# In contrast, the following condition would tell you if the request is for a valid form that is already defined to
# which one may want to add additional fields based upon conditional statements:
#
#if [template::form is_valid form_one] { }
#
# When any of the forms is submitted (and you can specify as many buttons as you like on each form), by default
# the values will be passed back to this tcl file for processing using the introspective facilities provides by
# template::form::get_button or template::form::get_action
#
# The structure of your add/edit page therefore is basically arranged around three modes controlled by conditional
# statements that direct execution according to the current state of the form accessible using the template::form api.


# OK, here we go....let's declare the forms
template::form create form_one
template::form create form_two

# By the way, if you add and -html {name value} to the end of the above you can specify any required form attributes
# such as javascript references and MIME specs as per oacs documentation on form-procs.tcl

# Check whether we arrived here from a previous form submission or if these are to be new forms
if {[template::form is_valid form_one]} {
    set button_clicked "Form One's '[template::form get_button form_one]' Button"
} elseif {[template::form is_valid form_two]} {
    set button_clicked "Form Two's '[template::form get_button form_two]' Button"
} else {
    
    # The forms have not previously been declared so now declare the form elements

    # Set this variable to avoid an error in the template
    set button_clicked "f"
    

    # Start of elements for form_one

    template::element create form_one form_one_id \
        -widget hidden \
        -datatype number \
        -value 1

    template::element create form_one form_one_field_one \
        -datatype text \
        -label "Field 1" \
        -html { size 10 maxlength 10} \
        -value ""

    template::element create form_one form_one_field_two \
        -widget textarea \
        -datatype text \
        -label "Field 2" \
        -html { rows 5 cols 80 wrap hard } \
        -value "You can specify default values"

    template::element create form_one form_one_field_three \
        -widget checkbox \
        -datatype boolean \
        -label "Field 3" \
        -options { { High t } } \
        -value "f"

    # End of elements for form_one


    # Start of elements for form_two

    template::element create form_two form_two_id \
        -widget hidden \
        -datatype number \
        -value 1

    template::element create form_two form_two_field_one \
        -datatype text \
        -label "Field 1" \
        -html { size 10 maxlength 10} \
        -value ""

     template::element create form_two form_two_field_two \
        -widget textarea \
        -datatype text \
        -label "Field 2" \
        -html { rows 8 cols 100 wrap hard } \
        -value "You can also specify style sheet classes or other html on a per element basis"

    template::element create form_two form_two_field_three \
        -widget checkbox \
        -datatype boolean \
        -label "Field 3" \
        -options { { High t } } \
        -value "f"
        
    # End of elements for form_two

}

ad_return_template

# TCL PAGE END - remove me

# ADP PAGE BEGIN - remove me

<master>
<property name="title">@title@</property>
<property name="context">@context@</property>

<h2>Ricky's little ad_form demo</h2>

<if @button_clicked@ eq "f">
  <h3>Form One</h1>
  <formtemplate id="form_one"></formtemplate>
  <h3>Form Two</h1>
  <formtemplate id="form_two"></formtemplate>
</if>
<else>
  <p>
    The following button was clicked:&nbsp;@button_clicked@
  </p>
  <p>
    To try me again click <a href="ad_form_demo">here</a>
  </p>
  <p>
    The documentation for template::form can be found <a href="/api-doc/proc-view?proc=template::form">here</a>
  </p>
</else>

# ADP PAGE END - remove me