====== Forms ====== Using the ReportForm class you can display a table as an html form. The ReportForm class will use the information in the registry to format the html form fields. If the default layout doesn't work for your user interface you can create an XML Form file that contains an HTML template describing how to display the form. ===== Display a Form from a table ===== Your table will need a registry file. Look in the registry/ directory to see if there is a file named .reg.xml. If not, go to the Registry section to see how to create a registry for your table. If the file exists, you can use the following example and replace the “product” table name with your table name. This example creates an html form from your table and associated registry file. setDefault(“product”); $f_products->setAddRecord(); $f_products->setForm(); $f_products->execute(); ?> The setDefault() method will generate a default form structure using the table “product” and the registry associate with it. For more details on how to display forms see the Install the form chapter. ===== Create a Form XML file ===== The registry controls what type of form field to use for each field of the table. But it doesn't describe where the field should be displayed or how it should be aligned. PAS Forms are HTML templates that display form fields. Forms are often linked to a table, to add or edit data from that table. The form files are stored in the form/ directory at the root of the application. You will find a blank template of a form file in : pas/templates/ Example : content of the file loginForm.form.xml :
Login Form :
]]>
User Name [username] Password [password] ]]>
The form file contains the following tags : idform: unique id of the form used as the primary key when the form is moved to the database, unused when forms are stored as XML files. name: Name of the form, this is the name used to create an instance of the form. descr: Description of the form. It is used in pagebuilder to explain to non-programming users what the form is and how to install it on a page. It can also include a template that explains how to insert the form in a page. idquery: Name of the query associated with the form. If there is no query the value is “0”. header: Header HTML template of the form. This is html that you can modify to change the layout of the form. row: Row HTML template of the form. This is html that you can modify to change the layout of the form. footer: Footer HTML template of the form. This is html that you can modify to change the layout of the form. ===== Install the form ===== To display the form on your web pages you need to use the ReportForm object. For example if you have created a form called: client registration, to display it you will use the following : setAddRecord(); $f_clientreg->setForm(); $f_clientreg->execute(); ?> The setAddRecord() will display an empty form ready to add data to the table. The call of setForm() method will generate the default events for that form. If you want the form to go to another page after the submission, for example, your thankyou.php page you can add : $f_clientreg->setUrlNext(“thankyou.php”) ; before the call of the setForm(). To edit the content of the form, you will need to provide the variable that will be used by the savedQuery to retrieve the data from the database. If your client table is called “client” you will probably have to set the variable: $idclient=. To know the name of the variable used in your form, look into the XML file that describes the form and under the tag you will find the name of the saved query used to retrieve the data. In that query you will find a variable in [], thats the variable needed. Most of the time its the primary key of that table. For example, to edit client number 5 using the client registration form: setForm(); $f_clientreg->execute(); ?> The line : global $idclient is not always required. (see variable scope). ===== Forms and Events ===== By default, forms will call the mydb.updateRecord or mydb.addRecord events but you may want to have the form send the content of the submission to another event. You can overwrite the default events with : setEvent(“myapp.recordClientData”, ”add”) ; $f_clientreg->execute(); ?> This form will call the event “myapp.recordClientData” instead of the default one. The content of the form(the html form fields) is in an array called: fields with fieldnames as key and content of the field as value. You can also keep the default events to add or update a record and add an extra event before or after the mydb.updateRecord and mydb.addRecord. For example, you have created an event that looks for bad words in the fields array and replaces them with “xxxx”. setAddRecord(); $f_clientreg->event->addEvent(“myapp.checkBadWords”, 5); $f_clientreg->setForm(); $f_clientreg->execute(); ?> The event is the event object associated with the form. The event->addEvent method has 2 parameters, the event name and the event level. To execute it before the default mydb.updateRecord or mydb.addRecord, the event level must be <1000. And to execute after > 1000. For more info on events, see the Events chapter. ===== Forms and Custom queries ===== When you display a form in a web page you can overwrite the default saved query with the following: setSavedQuery(“select one client”); $f_clientreg->event->addEvent(“myapp.checkBadWords”, 5); $f_clientreg->setQuery(); $f_clientreg->setForm(); $f_clientreg->execute(); ?> This will load the sql SavedQuery: select one client and to execute it you need to call the setQuery() method. ===== Forms and Custom fields ===== As you can overwrite the default sql saved query you can also overwrite the default registry when displaying the form. For example we want to set a hidden field of the form to a specific default value. To do that we will modify the registry object. Still using the same example we will get the following syntax setSavedQuery(“select one client”); $f_clientreg->event->addEvent(“myapp.checkBadWords”, 5); $f_clientreg->reg->fields['idpartner']->setRdatas(Array( "default" => "[getsession:idpartner:]", "hidden" => "1", "label" => "idpartner", "fieldtype" => "strFBFieldTypeInt")); $f_clientreg->setQuery(); $f_clientreg->setForm(); $f_clientreg->execute(); ?> This will set in the form the idpartner field as hidden with a default value from the session. The line: "[getsession:idpartner:]" could be simply replaced with $_SESSION['idpartner']. Alternatively you can create a custom registry file for the form and load it: $form_ApplicationForm->setRegistry("client_with_partner");