Trace:
Differences ¶
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:tutorials:simple-jforms-example [2008/03/04 14:18] – bibo | en: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 |
- | + | ||
- | 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 10: | Line 7: | ||
===== Create your application ===== | ===== Create your application ===== | ||
- | First, let's create your application. After donwloading | + | First, let's create your application. After downloading |
<code bash> | <code bash> | ||
- | ./jelix --myapp createapp | + | |
</ | </ | ||
- | Don't forget to change rights on '' | + | Don't forget to change rights on @@F@<jelix folder>/ |
- | An application skeleton is created. Your local tree structure | + | An application skeleton is created. Your local tree structure |
* myapp/ | * myapp/ | ||
Line 25: | Line 22: | ||
* classes/ | * classes/ | ||
* controllers/ | * controllers/ | ||
- | | + | * default.classic.php |
* dao/ | * dao/ | ||
* forms/ | * forms/ | ||
Line 35: | Line 32: | ||
* var/ | * var/ | ||
* www/ | * www/ | ||
- | * lib/ (jelix libraries) | + | * lib/ (Jelix libraries) |
* temp/ | * temp/ | ||
- | //myapp/// directory contains all files of your application. Note that there is another | + | @@F@myapp/@@ directory contains all files of your application. Note that there is another |
- | + | ||
- | Last, copy the content of // | + | |
+ | Last, copy the content of @@F@lib/ | ||
===== 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> | ||
<? | <? | ||
- | <forms xmlns=" | + | <form xmlns=" |
< | < | ||
Line 75: | Line 70: | ||
< | < | ||
</ | </ | ||
- | </forms> | + | </form> |
</ | </ | ||
Line 83: | Line 78: | ||
===== Displaying the form ===== | ===== Displaying the form ===== | ||
- | Now you're going to display the form. First, modify the default controller, | + | Now you're going to display the form. First, modify the default controller, |
<code php> | <code php> | ||
Line 94: | Line 89: | ||
$f = jForms:: | $f = jForms:: | ||
- | $view->tpl-> | + | $view->body-> |
return $view; | return $view; | ||
Line 101: | Line 96: | ||
</ | </ | ||
- | 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-> | + | 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-> |
- | In the **bodyTpl** property, | + | In the @@P@bodyTpl@@ property, |
- | 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 // | + | 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 |
- | You pass **$f** variable to an instance of a template engine (jTpl which works like smarty). It is stored in **$view-> | + | You pass @@V@$f@@ variable to an instance of a template engine (jTpl which works like smarty). It is stored in @@$view-> |
- | Last line returns | + | Last line returns |
- | Let's create this template in //myapp/ | + | Let's create this template in @@F@myapp/ |
<code php> | <code php> | ||
Line 118: | Line 113: | ||
</ | </ | ||
- | 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, | + | At this step, you can execute the action, |
- | thing because this action is the default one of the application), | + | |
+ | {{en: | ||
- | 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 |
<code html> | <code html> | ||
- | < | + | < |
- | < | + | < |
- | <input type=" | + | |
- | <input type=" | + | |
- | </ | + | |
< | < | ||
//< | //< | ||
Line 139: | Line 131: | ||
| | ||
| | ||
- | | + | |
- | | + | |
// (snip other javascript code...) | // (snip other javascript code...) | ||
// | // | ||
Line 172: | Line 164: | ||
</ | </ | ||
- | Note also that elements have CSS classes or IDs assigned to ease the 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> | ||
< | < | ||
- | {form $form, " | + | {form $form, " |
< | < | ||
< | < | ||
Line 192: | Line 184: | ||
</ | </ | ||
- | 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 identicals. The **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 : | ||
+ | |||
+ | |||
+ | {{en: | ||
+ | |||
+ | |||
Line 200: | 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. | + | Now you're going to create the action method indicated in the arguments of the **form** plugin. |
<code php> | <code php> | ||
Line 209: | Line 206: | ||
$f = jForms:: | $f = jForms:: | ||
if(!$f || !$f-> | if(!$f || !$f-> | ||
- | $rep = $this-> | + | $view = $this-> |
- | $rep-> | + | $view-> |
- | return $rep; | + | return $view; |
} | } | ||
Line 220: | Line 217: | ||
</ | </ | ||
- | 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' | + | 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' |
<code php> | <code php> | ||
Line 228: | Line 225: | ||
</ | </ | ||
- | Instead of just creating a new instance of the contact form, check before if there is an existing one. | + | 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-> | + | Back to @@M@save()@@ method. After the @@$f-> |
<code php> | <code php> | ||
Line 236: | Line 233: | ||
$alldatas = $f-> | $alldatas = $f-> | ||
+ | // or | ||
$firstname = $f-> | $firstname = $f-> | ||
- | if ($firstname==' | + | if ($firstname == 'E.T.') { |
+ | | ||
+ | | ||
// and so on. | // and so on. | ||
</ | </ | ||
- | 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 259: | Line 259: | ||
</ | </ | ||
- | Use again the **formcontrols** plugin, but in a standalone manner (not with a **form** plugin). Indicate as arguments | + | Use again the **formcontrols** plugin, but in a standalone manner (not with a **form** plugin). Indicate as arguments |
+ | |||
+ | {{en: | ||
+ | |||
+ | |||
+ | |||
+ | |||
===== Conclusion ===== | ===== Conclusion ===== | ||
- | That's all ! We saw how it is easy with jForms : | + | That's all! It is really |
+ | |||
+ | * create a form | ||
+ | * display a form | ||
+ | * retrieve submitted data | ||
+ | * validate submitted data (this is done automatically !) | ||
+ | * display the form again with errors | ||
- | * to create a form | + | With only one xml file and few lines of code! |
- | * to show a form | + | |
- | * to retrieve submitted datas | + | |
- | * to validate submitted datas (this is done automatically | + | |
- | * to show the form again with errors | + | |
- | Notice that jForms has many other features, | + | And jForms has many other features, |
- | * to create your own form " | + | * create your own form " |
- | * to specify your own error messages | + | * specify your own error messages |
- | * to manage how error messages are displayed | + | * localize everything |
- | * to display helps and tooltip | + | * manage how error messages are displayed |
- | * to fill menulist, listbox | + | * display helps and tooltips |
- | * to store automatically | + | * fill menulist, listbox, (and so on) choices |
- | * to create quickly a CRUD controller, by using the jControllerDaoCrud controller (only indicate a jforms file, a jdao file, and that's all !) | + | * read and store data automatically |
- | * and many other controls are availabled: **< | + | * create quickly a CRUD controller, by using the @@C@jControllerDaoCrud@@ controller (only indicate a jforms file, a jdao file, and that's all !) |
- | * some others will be available soon: | + | * and many other controls are availabled: **< |