Quick links: Content - sections - sub sections
EN

Trace: generic1 1.2beta1 1.2.1 1.1.2 1.1rc1 1.2.x

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:tutorials:simple-jforms-example [2008/11/19 15:02] – external edit 127.0.0.1en:tutorials:simple-jforms-example [2012/12/06 20:38] (current) – [Simple jForms example for Jelix 1.3] laurent
Line 1: Line 1:
-====== Simple jForms example ====== +====== Simple jForms example for Jelix ======
- +
-Here is a little tutorial to demonstrate how powerful is jForms, the Jelix form system. +
- +
  
 +Here is a little tutorial to demonstrate how powerful is jForms, the Jelix form system. This tutorial is compatible with Jelix 1.3 and 1.4.
  
  
Line 13: Line 10:
  
 <code bash> <code bash>
-  ./jelix --myapp createapp+  php createapp.php ../myapp
 </code> </code>
  
-Don't forget to change rights on ''<jelix folder>/temp/myapp/''. This will allow your web server to create files in this directory([[en:manual-1.0:installation|see installation manual]]).+Don't forget to change rights on @@F@<jelix folder>/temp/myapp/@@. This will allow your web server to create files in this directory ([[http://docs.jelix.org/en/manual-1.3/getting-started|see installation manual]] for details).
  
 An application skeleton is created. Your local tree structure should look like: An application skeleton is created. Your local tree structure should look like:
Line 25: Line 22:
           * classes/           * classes/
           * controllers/           * controllers/
-          * default.classic.php+             * default.classic.php
        * dao/        * dao/
        * forms/        * forms/
Line 39: Line 36:
  
  
-//myapp/// directory contains all files of your application. Note that there is another //myapp/// folder in //myapp/modules/// : by default **createapp** command creates a module with the same name of your application. This will be the main module. +@@F@myapp/@@ directory contains all files of your application. Note that there is another @@F@myapp/@@ folder in @@F@myapp/modules/@@: by default the **createapp** command creates a module with the same name of your application. This will be the main module.
- +
-Last, copy the content of //lib/jelix-www/// into //myapp/www/jelix// (or can declare an alias in apache, if preferred). +
  
 +Last, copy the content of @@F@lib/jelix-www/@@ into @@F@myapp/www/jelix@@ (or you can declare an alias in apache, if preferred).
  
 ===== The contact form ===== ===== The contact form =====
  
  
-Now you're going to create a form. The only thing to do, is to create one XML file in the //forms/// directory. Call it contact.form.xml. Here is its content:+Now you're going to create a form. The only thing to do, is to create one XML file in the @@F@forms/@@ directory. Call it @@F@contact.form.xml@@. Here is its content:
  
 <code xml> <code xml>
  <?xml version="1.0" encoding="utf-8"?>  <?xml version="1.0" encoding="utf-8"?>
- <form xmlns="http://jelix.org/ns/forms/1.0">+ <form xmlns="http://jelix.org/ns/forms/1.1">
    
    <menulist ref="title">    <menulist ref="title">
Line 79: Line 74:
  
 You can see that it is a declarative format. It is easy to read and edit. You can see that it is a declarative format. It is easy to read and edit.
- 
- 
  
  
 ===== Displaying the form ===== ===== Displaying the form =====
  
-Now you're going to display the form. First, modify the default controller, //default.classic.php//:+Now you're going to display the form. First, modify the default controller, @@F@default.classic.php@@:
  
 <code php> <code php>
Line 103: Line 96:
 </code> </code>
  
-On the first line, you retrieve an HTML view (a jHtmlResponse object) with the **getResponse()** method. Next, you set the title of the page (**$view->title**). +On the first line, you retrieve an HTML view (a @@C@jHtmlResponse@@ object) with the @@M@getResponse()@@ method. Next, you set the title of the page (@@$view->title@@). 
-In the **bodyTpl** property,  you indicate the template to use for the main content of the page (the content of the body tag of a HTML page; HTML header is generated automatically by the view). The given string is called a __selector__ in Jelix. A selector specifies a resource in a module. Its syntax is "moduleName~resourceName". Here, you simply say that the template will be //templates/main.tpl// file in //myapp// module.+In the @@P@bodyTpl@@ property,  you indicate the template to use for the main content of the page (the content of the body tag of a HTML page; HTML header is generated automatically by the view). The given string is called a __selector__ in Jelix. A selector specifies a resource in a module. Its syntax is "moduleName~resourceName". Here, you simply say that the template will be @@F@templates/main.tpl@@ file in //myapp// module.
  
-Next, in the **$f** variable, you put an instance of the contact form. //jForms// creates dynamically a class from the content of contact.form.xml and //create// static method returns an instance of this class. Note that this class inherits from the //jFormsBase// class.+Next, in the @@V@$f@@ variable, you put an instance of the contact form. jForms creates dynamically a class from the content of @@F@contact.form.xml@@ and the static method @@M@create()@@ returns an instance of this class. Note that this class inherits from the @@C@jFormsBase@@ class.
  
-You pass **$f** variable to an instance of a template engine (jTpl which works like smarty). It is stored in **$view->tpl**.+You pass @@V@$f@@ variable to an instance of a template engine (jTpl which works like smarty). It is stored in @@$view->tpl@@.
  
-Last line returns **$view** object and Jelix **finishes the job** : the view will generate a page based on the given template file, //main.tpl//.+Last line returns @@V@$view@@ object and Jelix **finishes the job**: the view will generate a page based on the given template file, @@F@main.tpl@@.
  
-Let's create this template in //myapp/templates/// directory :+Let's create this template in @@F@myapp/templates/@@ directory:
  
 <code php> <code php>
Line 120: Line 113:
 </code> </code>
  
-It uses **formfull** template plugin, which accepts as arguments, a **jFormsBase** object, and a selector of action. this is the action to be called after a submit on the form.+It uses the **formfull** template plugin, which accepts as arguments, a @@C@jFormsBase@@ object, and a selector of action. this is the action to be called after a submit on the form.
  
-At this step, you can execute the action, **http://localhost/jelix/myapp/www/**  (or **http://localhost/jelix/myapp/www/index.php?module=myapp&action=default:index**, this is the same +At this step, you can execute the action, @@http://localhost/jelix/myapp/www/@@  (or @@http://localhost/jelix/myapp/www/index.php/myapp/default/index@@, this is the same thing because this action is the default one of the application), and watch the results:
-thing because this action is the default one of the application), and watch the results :+
  
 {{en:tutorials:simple-forms-example-1.png}} {{en:tutorials:simple-forms-example-1.png}}
  
-When testing this form, notice the check done on fields before submit : some javascript code is generated automatically on this purpose. Anyway, here is the generated html code :+When testing this form, notice the check done on fields before the submit: some javascript code is generated automatically on this purpose. Anyway, here is the generated html code:
  
 <code html> <code html>
- <form action="/jelix/myapp/www/index.php" method="post" id="jform1" onsubmit="return jForms.verifyForm(this)"> + <form action="/jelix/myapp/www/index.php/myapp/default/save" method="post" id="jform1" onsubmit="return jForms.verifyForm(this)"> 
- <div> + <div></div>
-      <input type="hidden" name="module" value="myapp"/> +
-      <input type="hidden" name="action" value="default:save"/> +
- </div>+
  <script type="text/javascript">  <script type="text/javascript">
  //<![CDATA[  //<![CDATA[
Line 177: Line 166:
 Note also that form elements have CSS classes or IDs assigned to ease their design.  Note also that form elements have CSS classes or IDs assigned to ease their design. 
  
-**formfull** plugin generates the form in an HTML table. Of course there are alternatives. By using other plugins, Jelix allows you to control precisely the html generated. Let's change that in the main.tpl file.+The **formfull** plugin generates the form in an HTML table. Of course there are alternatives. By using other plugins, Jelix allows you to control precisely the html generated. Let's change that in the main.tpl file.
  
 <code html> <code html>
  <h2>jForm example</h2>  <h2>jForm example</h2>
- {form $form, "myapp~default:save", array(),'','','post'}+ {form $form, "myapp~default:save", array(),'html', array('method'=>'post')}
   <fieldset>   <fieldset>
      <legend>Please fill out this form:</legend>      <legend>Please fill out this form:</legend>
Line 195: Line 184:
 </code> </code>
  
-The template above use **form** plugin instead of **formfull**. Its arguments are identical. **formcontrols** plugin do a loop over the fields declared in the contact.form.xml (except submit and reset controls), and you can display the label and the field with **ctrl_label** and **ctrl_control**. **formsubmit** displays all submit controls.+The template above use the **form** plugin instead of **formfull**. Its arguments are identicalsThe **formcontrols** plugin do a loop over the fields declared in the @@F@contact.form.xml@@ file (except submit and reset controls), and you can display the label and the field with **ctrl_label** and **ctrl_control**. **formsubmit** displays all submit controls.
  
 Here is the result of this template : Here is the result of this template :
Line 208: Line 197:
 ===== Handling submitted data ===== ===== Handling submitted data =====
  
-Now you're going to create the action method indicated in the arguments of the **form** plugin. In the templates above, it is called 'save'. Add this method in default.classic.php.+Now you're going to create the action method indicated in the arguments of the **form** plugin. In the templates above, it is called 'save'. Add this method in @@F@default.classic.php@@.
  
 <code php> <code php>
Line 228: Line 217:
 </code> </code>
  
-To get the instance of the form, and by the same time to fill it with submitted data, use the **fill** method. Then, test if this instance doesn't exist (**!$f**) or if there are bad data (**!$f->check()**). If this is the case, make a redirection to the first action (displaying the form). The form will be displayed again with error messages included: this is automatic ! However, you should change slightly the index method. Replace **$f = jForms::create("myapp~contact");** by +To get the instance of the form, and by the same time to fill it with submitted data, use the @@M@fill()@@ method. Then, test if this instance doesn't exist (@@!$f@@) or if there are bad data (@@!$f->check()@@). If this is the case, make a redirection to the first action (displaying the form). The form will be displayed again with error messages included: this is automatic! However, you should change slightly the index method. Replace @@$f = jForms::create("myapp~contact");@@ by 
  
 <code php> <code php>
Line 236: Line 225:
 </code> </code>
  
-Instead of just creating a new instance of the contact form, check before if there is an existing one. So HTML fields will be filled with datas previously submitted.+Instead of just creating a new instance of the contact form, check before if there is an existing one. So HTML fields will be filled with datas previously submitted (and saved in the session).
  
-Back to **save** method. After the **$f->check()**, you could insert some business code (not shown in this tutorial). For example:+Back to @@M@save()@@ method. After the @@$f->check()@@, you could insert some business code (not shown in this tutorial). For example:
  
 <code php> <code php>
Line 246: Line 235:
     // or     // or
     $firstname = $f->getData('firstname');     $firstname = $f->getData('firstname');
-    if ($firstname=='ET') { /* goto home */ }+    if ($firstname == 'E.T.') {  
 +        /* goto home */ 
 +    }
     // and so on.     // and so on.
 </code> </code>
  
-Finally, you could display submitted data and thanks the user. Here is the contact_ok.tpl file:+Finally, you could display submitted data and thanks the user. Here is the @@F@contact_ok.tpl@@ file:
  
 <code html> <code html>
Line 268: Line 259:
 </code> </code>
  
-Use again the **formcontrols** plugin, but in a standalone manner (not with a **form** plugin). Indicate as arguments the jFormsBase object (**$form**), and the list of controls to display (those are optional).+Use again the **formcontrols** plugin, but in a standalone manner (not with a **form** plugin). Indicate as arguments your form object (@@$form@@), and the list of controls to display (those are optional).
  
 {{en:tutorials:simple-forms-example-3.png}} {{en:tutorials:simple-forms-example-3.png}}
Line 280: Line 271:
  
  
-That's all ! It is really easy to use jForms, isn't it? You can :+That's all! It is really easy to use jForms, isn't it? You can:
  
   * create a form   * create a form
Line 288: Line 279:
   * display the form again with errors   * display the form again with errors
  
-With only one xml file and few lines of code !+With only one xml file and few lines of code!
  
-And jForms has many other features, such as :+And jForms has many other features, such as:
  
-  * create your own form "builder" (soon in jelix 1.1), so plugins could displays the form, not in HTML, but in other format like Xforms, XUL, or in HTML + your prefered ajax library.+  * create your own form "builder", so plugins could displays the form, not in HTML, but in other format like Xforms, XUL, or in HTML + your prefered ajax library.
   * specify your own error messages   * specify your own error messages
   * localize everything   * localize everything
   * manage how error messages are displayed   * manage how error messages are displayed
   * display helps and tooltips   * display helps and tooltips
-  * fill menulist, listbox, (and so on) choices automatically, using a jDao object (ORM layer of Jelix)+  * fill menulist, listbox, (and so on) choices automatically, using a @@C@jDao@@ object (ORM layer of Jelix)
   * read and store data automatically still using a jDao object   * read and store data automatically still using a jDao object
-  * create quickly a CRUD controller, by using the jControllerDaoCrud controller (only indicate a jforms file, a jdao file, and that's all !) +  * create quickly a CRUD controller, by using the @@C@jControllerDaoCrud@@ controller (only indicate a jforms file, a jdao file, and that's all !) 
-  * and many other controls are availabled: **<textarea>**, **<listbox>**, **<checkbox>**, **<checkboxes>**, **<radiobuttons>**, **<reset>**,   **<secret>**, **<secret>**+confirmation, **<upload>** +  * and many other controls are availabled: **<textarea>**, **<htmleditor>**,  **<wikieditor>**, **<captcha>**, **<listbox>**, **<checkbox>**, **<checkboxes>**, **<radiobuttons>**, **<reset>**,   **<secret>****<secret>**+confirmation, **<upload>** etc...
-  * some others will be available soon in Jelix 1.1 :  **<htmleditor>** **<hidden>** **<captcha>** (already in 1.1a1pre) etc.+
  
  

en/tutorials/simple-jforms-example.1227106958.txt.gz · Last modified: 2012/02/01 10:19 (external edit)

Recent changes RSS feed Creative Commons License