Form Manager Overview

top headingForm manager ships with LogiCreate 2.9 and later releases. Form manager was built to provide developers a way to handle form creation, validation and error handling. Let's face it, our lives as a web application developers are mostly spent taking input from one user, storing it in a database and retrieving that information for another user. Error checking a forty field form can be a tedious process and creating complex drop downs that re-select themselves if there is a user error can be time consuming. Form manager addresses these and other problems and tries to take the work out of creating forms for a LogiCreate developer. The next few sections we'll cover how to create forms in LogiCreate using this tool and give you some advanced uses of how you can extend form manager to meet your site's needs.

Features

Using the interface

Site Forms

hercules form manager opening pageThe interface to create, add, and modify forms is accessible via the Hercules Control Center (which is included in LogiCreate). In the Site menu in Hercules (located on the left hand side of the screen) locate the link that says Site Forms. Select this link by clicking on it. As normal with the Hercules control panel, the interface for the application will appear in the right hand side of the screen.

At the top of the screen you will see two links: Site Forms and Add a New Form. The Site Forms link will take you back to the screen you are on. We'll touch more on Add a New Form later on. Below this top section you will see 'Existing Forms'. This area will display all of the forms you have created using form manager. To see the fields and modify the properties as well as add new form elements click on the link Modify.

Form Fields

form fields opening screenUnderneath the link Site Forms in the site control panel you'll see the link Form Fields. Pressing this link will take you to a list of all of the different types of form fields which you can add to your forms. In the list you will see some of the following: add a form, text input, textarea, etc. Each of these fields are essentially self contained forms. In other words when you modify the 'text input' form, what you are modifying is the form that allows you to create a text input field in form manager. Unless you really know what you are doing we suggest to not modify any of these forms as doing say may break the forms on your site. Consider this to be an administrative control panel for Form Manager.

The form fields section is used to create the form fields in your forms. Let's say for example you wanted to add a 'phone' field so you can easily add it to your forms. You would first create a new form and give it the type 'phone'. You would then add the specific fields you wanted to create for a phone field for your forms. Another way to accomplish this is to create a "custom field" which we'll cover later on.

Pre-built Forms

Form manager ships with two pre-built forms: Contact Us and User Profile. If you are using the open source version of LogiCreate you can download the 'contactus' module located in the document library at http://www.logicreate.com. The contact us form is a very simple form which can be used to allow people to contact different departments of your business online. By installing the contact us module it will use this form automatically. The user profile form is used in the include LogiCreate module 'users' and is called by the service 'editProfile.lcp'. Feel free to modify these two forms to suite your needs for your site.

Creating a new form

At the top of the main page is the link Add a New Form. Pressing this link will take you to the form which allows you to create new forms. Let's cover the different options for this screen.

FIELD NAME DESCRIPTION
Enter a name for your form Give your form a name. Example given is "User Profile". It is a good idea to prefix your forms with a grouping tag to make them easy to find. For example, if the form you are creating will be used in the control center you may want to name the form "Herc - Add a FAQ".
Enter a description Explain what your form is and how it is used. Example: "Used for adding / editing grade book records".
Enter the form method Select either GET or POST (if you select GET your form values are passed in the URL, using POST they are not).
Form Type Set the field type to site unless you are creating a custom form field. If you do not enter site into this field, your form will appear in the list under Site Forms. If you are creating a new form element for example 'mytextinput' you would enter 'mytextinput' (this type will be used in your object to associate rendering and validation of your form field type).
Form Code As a developer it is easier for us to remember string codes such as 'contactus' or 'shopcart1'. On this field enter the code you will use to pass to the getForm() method. Example: $f->getForm('contactus').
Table width This is a table property. You can enter the pixel value such as 650 or percentages such as 75%.
Table border width Select if you want a border in your table.
Table cellspacing Select the amount of cell spacing you wish to have in your table
Table cellpadding Select the amount of cell padding you wish to have in your table
Enter the target of your form If you are using frames you may need to specify the target of your form. Enter that here.
Enter the action of your form When a user presses the submit button, where should it send the GET or POST values? In LogiCreate you can enter the module and event you are submitting to or you can include a hidden event field in your form and leave this field blank.
Form encoding type Select the encoding type of form. Take the default unless you are allowing people to upload pictures or other information into your form. In this case select multipart/form-data.

Enter the above information as need to create the initial foundation for your form. Essentially this is creating all of your default form properties of the form object (most all of which you can change inside your code at run-time if needed).

Adding fields to your form

Once you have created a form you are ready to start placing fields onto your form. From the main Site Forms page, the form you just created should appear in the list. Select the Modify link to the right of your new form. The next screen will give you a lot of different options. Let's cover what each of these are.

Form Management

form management interface

LINK DESCRIPTION
Site Forms Takes you back to main interface
Modify Form Allows you to adjust the properties you entered for your form (action, method, name, description, etc)
Reset Sorting As you develop forms there will be times when you need to go back and add a new field. The blue arrows next to each form field allow you to sort which order fields are displayed. If for some reason the sorting gets out of sync pressing this link will reset the fields in the order they were created.
Preview Form If you would like to see what you form will look like (without style sheets applied or anything) press this link. A java script pop-up window will appear with your form rendered.
View HTML Source Does exactly what it says. It displays the HTML source code of your form. If you like our interface for creating forms but are not a developer you can use this (note the HTML isn't formatted).
Delete Form Will permanently remove your form and all of the fields. This action is non-reversible, only use it if you are absolutely sure you are viewing the correct form and it isn't being used in your site.

This section will appear at the top of any form you are modifying.

Click below to add a Form Field to this form

add a form field section

Press any of the above options to add this field type to your form. As a web developer these should be self-explanatory. There are two special form fields listed above that we will cover that are form manager specific.

Custom fields - If you wish to create custom form fields for your site you will use the Custom Fields option. See the 'Creating Custom Fields' section for developer information on how to accomplish this.

Row - Although row is not a form field we use it to add form labels or help information to your forms. You can attach a style sheet to a row for extra decoration as well. For example if you have 15 fields on a form, usability testing shows it is best to break these up into 3 blocks of 5. With this in mind you may want to start with a row with "Step One - User information" and then later on add another row "Step Two - Optional Information" and so on.

Date Drop Down - Date drop down is a special form field created in form manager to handle just about any type of date or time you may need. Although it is a very advanced use of an extra field type, it is very useful and easy to use. Date drop down supports the following fields: month, day, year, hour, minutes, am/pm. You can select any combination of the fields needed and also determine the start and end dates for your year. The date drop down method in the form object will accept almost any type of date and pre-populate the drop downs with that date.

Check box - Creates a set of check box options in your form. This option creates one, or multiple check boxes.

Radio button - Creates a set of radio buttons options in your form. This option can be used to create one or multiple radio button options.

Submit button - Creates a submit button. (using images on submit buttons not currently supported)

Text Area - Creates a text area on your form. Also allows you to select the WYSIWYG option.

File Upload - Creates a form option to upload a file via a form.

Text Input - Creates a normal input form field.

Hidden - Creates a hidden field. In LogiCreate this is used primarily for triggering your next event.

Select - Creates different select options such as drop downs or multiple selects.

 

Creating a Form Field

There are a set of standard fields that we need to cover so you understand how to create the different form fields listed above. Below we cover most of the options when building a form. Not all of the options below are on every element.

Creating Custom Fields

As you create more complex forms you will find yourself needing to pull information from a database and render a dynamic drop or other information for example. Form Manager has the ability to accommodate the need for pulling dynamic information and displaying it in the form. The benefit for a developer, is once you create the custom field, you can apply it to any form.

Add field to drop down

  1. Let's add the field to form manager so it can be selected in the "Custom Fields" option. To do this, select "Form Fields" in the site menu of Hercules.
  2. Find the "Custom Fields" form and select "Modify".
  3. Locate the field type of 'select' labeled as 'Select field type (extra)' and modify this field.
  4. At the bottom of the page, locate the 'Enter your drop down options' field. Here you are going to enter the name of your custom function, and the drop down name. The 'key' corresponds to the name of the ToHTML method in the SiteForms.php object located in the lib directory. As an example, you should see groups=System Groups already listed. The key 'groups' corresponds directly to the method groupsToHTML() located in SiteForms.php. Name your key the name of your method but leave off the ToHTML as it is added automatically for you.
  5. Save this change.
  6. If you try adding a custom field to your form, you will now have the option you created.

Creating the method

After you get your new field added to form manager, it is time to start writing what you need to pull or generate. Here is the sample groupsToHTML().

 

function groupsToHTML($v)
{

# select the information you need
$this->db->query("select * from lcGroups");

# loop through that information and build the string for the drop down
while ($this->db->next_record() ) {
$arr .= $this->db->Record[gid].'='.$this->db->Record[groupName].',';
}
$v['selectOptions'] = substr($arr, 0, -1);

# call the proper function to render the type of field you need
$HTML = $this->selectToHTML($v);
return $HTML;
}

 

The magic happens in the while loop above. We are pulling back the records we need and then are taking the group ids, and building the key / val string the way we normally do when creating select drop downs. The variable $v that is passed in, holds all of our form specific data. We simply override the $v['selectOptions'] element of the array with the data we need to use.

Integrating Forms Into Your Site / Applications

This section covers how to integrate form manager into your applications and your site. We are going to use the 'contactus' module which can be downloaded from the LogiCreate site via the document library as a starter example. If you haven't built or familiarized yourself with how LogiCreate applications or modules are built you will need to do so before continuing. This section assumes you understand how to build LogiCreate modules and how they are used.

Loading a form

Include SiteForms.php
The first step is to add the following to the top of your LCP file:

include_once(LIB_PATH.'SiteForms.php');

This will provide all of the code you need in order to use form manager. In fact, SiteForms.php includes the main library of LC_forms.php located in the lib directory.

Add a cleaned array property (optional)
You may or may not want to do this depending on how you are using form manager, but for starting out we recommend it. Adding this cleaned array property to your LCP object allows you to have access to the data in the object that has been cleaned after validation. When you validate a form manager form, it automatically cleans the input for you and doesn't return fields that are not allowed in the form. This protects against cross site attacks.

Example: var $cleanedArray = array();

Using the object
Here is the example used in the contact us module.

 

$f = new SiteForm();
$f->getForm('contactus', $this->cleanedArray);
$t['form'] = $f->ToHTML();

 

Contact Us form imageLet's look at the above example. First we instantiate the object. Next we load up the form via the getForm() method and pass it two parameters. The first one is the 'name' of the form. The second is the cleaned array. The second parameter to getForm() is optional. It is a key / value array of the data that we want to pre-populate our form with. The key's correspond to the field names. The first time this is run, there is nothing in $this->cleanedArray. As we'll see in the few sections, if there is an error after validation, we want to pre-populate the form with the information the user inputted. Whether you have a 3 or a 40 field form, the above three lines of code are all you need to display a form in LogiCreate. Notice that we are storing the form in the template variable so that we only have to echo out $t['form'] in our template. Form manager takes care of the rest.

Validating the Form Input

Once the user submits a form, your event in LogiCreate needs to validate the form input. If it doesn't pass inspection you will want to redirect the user back to the form, otherwise you continue on in your application. The following code shows you how this is done. In the 'contactus' module, this is called emailRun.

# load the form manager object
$f = new SiteForm();
# validate the form
$t['error'] = $f->validateForm('contactus', $arg->postvars);

# if there are errors, send them back to the run event
if ($f->hasErrors() )
{
# take the cleaned data from the form object and store in the LCP object
$this->cleanedArray = $f->cleanedArray;
# send the user back to run
$this->run($db, $u, $arg, $t);
return;
}

 

Contact us errorOnce you instantiate the object call the validateForm() method and pass it the name of the form you wish to validate, and the information in the post var array ($arg->postvars). The validateForm() method loads up the form and then does a series of checking and cleaning the input. The result of this automated error checking returns an error message. As seen above we capture this error message in the template variable. Any information that was entered or selected by the user would automatically be saved as well. The way this gets saved and pushed back to the user is by grabbing the cleaned array from the form object and storing it into the LCP object.

Cleaned Array
The cleaned array that we mentioned earlier is what allows us to give the information back to the user if there is an error. If you notice in the first example we looked at $this->cleanedArray being passed as the second argument.

$f = new SiteForm();
$f->getForm('contactus', $this->cleanedArray);
$t['form'] = $f->ToHTML();

The cleaned array gets placed back into the object in your submission event. If you need to load information from a database to pre-populate a form, the same method is used.