Help Tips « back
What are conditional sections and where can I use them?


The conditional sections (field sets in the form specification) allow the developer to use the same XML specification for a form that can be viewed with different sets of fields depending on the role of the user. It's an implementation of access control in the HTML forms.

In the XML specification, you can use the tag SECTION, defining the attribute CONDITION as "T" and providing an evaluate function to return TRUE (the section will be generated) or FALSE (the section is hidden).

To understand the applicability of this feature, consider the following example:

1) You build a system that sells products on the Internet - softwares, for example;
2) The common user can fill a user form an login into the system;
3) When this common user enters the "buy" page, he fills only the fields that are related to the product he wants to buy;
4) However, when an administrator runs the same page, it would be great if it could choose the user before providing the product information in the form.

To get this feature to work, you would have to do something like this:

User conditional section:

// in the XML file
<condsection id="user_section" evalfunction="evaluateFormSections">
  <hiddenfield name="ID_USER" default="mySessionObject:ID_USER"/>
</condsection>
// in the template file
<!-- START BLOCK : user_section -->
{ID_USER}
<!-- END BLOCK : user_section -->

Admin conditional section:
// in the XML file
<condsection id="admin_section" evalfunction="evaluateFormSections">
  <lookupfield name="ID_USER" label="Choose an user" required="T">
	<datasource>
	  <keyfield>ID_USER</keyfield>
	  <displayfield>NAME</displayfield>
	  <lookuptable>USERS</lookuptable>
	  <clause>ACTIVE = 1</CLAUSE>
	  <orderby>NAME</orderby>
	</datasource>
  </lookupfield>
</condsection>
// in the template file
<!-- START BLOCK : admin_section -->
<tr>
  <td>{label_ID_USER}<br>{ID_USER}</td>
</tr>
<!-- END BLOCK : admin_section -->
PHP Evaluation function (visible from the point where the form is generated):

<?php
function evaluateFormSections(&$section) {
    
// the use of static variables is good when you evaluate
    // different sections using the same function result
    
static $currentRole;
    if (!isset(
$currentRole)) {
        
$currentRole = myFunctionThatGetsTheCurrentRole();
    }
    switch (
$section->getId()) {
        case
'user_section' : return ($currentRole == 'user'); break;
        case
'admin_section' : return ($currentRole == 'admin'); break;
        default : return
FALSE;
    }
}
?>