Quick links: Content - sections - sub sections
EN

Trace:

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
Next revisionBoth sides next revision
en:tutorials:simple-jforms-example [2008/03/04 14:18] biboen:tutorials:simple-jforms-example [2008/11/19 15:02] – external edit 127.0.0.1
Line 10: Line 10:
 ===== Create your application ===== ===== Create your application =====
  
-First, let's create your application. After donwloading a [[http://jelix.org/articles/en/download/stable|jelix archive]], extract it to your <jelix folder> of choice. Navigate to ''<jelix folder>/lib/jelix-scripts/'' directory and type in a console:+First, let's create your application. After downloading a [[http://jelix.org/articles/en/download/stable|jelix archive]], extract it to your <jelix folder> of choice. Navigate to ''<jelix folder>/lib/jelix-scripts/'' directory and type in a console:
  
 <code bash> <code bash>
Line 16: Line 16:
 </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:installation|see installation manual]]).+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]]).
  
-An application skeleton is created. Your local tree structure shall look like:+An application skeleton is created. Your local tree structure should look like:
  
   * myapp/   * myapp/
Line 35: Line 35:
     * var/     * var/
     * www/     * www/
-  * lib/   (jelix libraries)+  * lib/   (Jelix libraries)
   * temp/   * temp/
  
Line 48: Line 48:
  
  
-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 //forms/// directory. Call it 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"?>
- <forms xmlns="http://jelix.org/ns/forms/1.0">+ <form xmlns="http://jelix.org/ns/forms/1.0">
    
    <menulist ref="title">    <menulist ref="title">
Line 75: Line 75:
      <label>Contact us</label>      <label>Contact us</label>
    </submit>    </submit>
-  </forms>+  </form>
 </code> </code>
  
 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.
 +
 +
  
  
Line 94: Line 96:
    
         $f = jForms::create("myapp~contact");         $f = jForms::create("myapp~contact");
-        $view->tpl->assign('form', $f );+        $view->body->assign('form', $f );
    
         return $view;         return $view;
Line 102: Line 104:
  
 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 jHtmlResponse object) with the **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; the 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 **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.
  
 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 **$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.
Line 123: Line 125:
 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}}
  
 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 submit : some javascript code is generated automatically on this purpose. Anyway, here is the generated html code :
Line 139: Line 142:
  jForms.tControl = new jFormsControl('title', 'Title', 'string');  jForms.tControl = new jFormsControl('title', 'Title', 'string');
  jForms.tControl.required = true;  jForms.tControl.required = true;
- jForms.tControl.errRequired='La saisie de "Title" est obligatoire'; + jForms.tControl.errRequired='"Title" input is required'; 
- jForms.tControl.errInvalid ='La saisie de "Title" est invalide';+ jForms.tControl.errInvalid ='"Title" input is invalid';
  // (snip other javascript code...)  // (snip other javascript code...)
  //]]>  //]]>
Line 172: Line 175:
 </code> </code>
  
-Note also that elements have CSS classes or IDs assigned to ease the design of the form+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. **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.
Line 195: Line 198:
  
 Here is the result of this template : Here is the result of this template :
 +
 +
 +{{en:tutorials:simple-forms-example-2.png}}
 +
 +
  
  
Line 200: Line 208:
 ===== Handling submitted data ===== ===== Handling submitted data =====
  
-Now you're going to create the action method indicated in the arguments of the **form** plugin. Reffering to 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 default.classic.php.
  
 <code php> <code php>
Line 209: Line 217:
         $f = jForms::fill("myapp~contact");         $f = jForms::fill("myapp~contact");
         if(!$f || !$f->check()) {         if(!$f || !$f->check()) {
-            $rep = $this->getResponse('redirect'); +            $view = $this->getResponse('redirect'); 
-            $rep->action="myapp~default:index"; +            $view->action="myapp~default:index"; 
-            return $rep;+            return $view;
         }         }
    
Line 228: Line 236:
 </code> </code>
  
-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.
  
 Back to **save** method. After the **$f->check()**, you could insert some business code (not shown in this tutorial). For example: Back to **save** method. After the **$f->check()**, you could insert some business code (not shown in this tutorial). For example:
Line 236: Line 244:
  
     $alldatas = $f->getDatas();     $alldatas = $f->getDatas();
 +    // or
     $firstname = $f->getData('firstname');     $firstname = $f->getData('firstname');
-    if ($firstname=='ET') { /goto home }+    if ($firstname=='ET') { /goto home */ }
     // and so on.     // and so on.
 </code> </code>
Line 260: Line 269:
  
 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 the jFormsBase object (**$form**), and the list of controls to display (those are optional).
 +
 +{{en:tutorials:simple-forms-example-3.png}}
 +
 +
 +
 +
 +
  
 ===== Conclusion ===== ===== Conclusion =====
  
  
-That's all ! We saw how it is easy with jForms :+That's all ! It is really easy to use jForms, isn't it? You can : 
 + 
 +  * 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, which allow you :+And jForms has many other features, such as :
  
-  * to create your own form "builder", so plugins could displays the form, not in HTML, but in other format like Xforms, XUL, or in HTML+ajax etc+  * 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
-  * 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 etc automatically, from a jDao object (the ORM layer of Jelix) +  * display helps and tooltips 
-  * to store automatically datas with a jDao object +  * fill menulist, listbox, (and so on) choices automatically, using a jDao object (ORM layer of Jelix) 
-  * 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 still using a jDao object 
-  * and many other controls are availabled: **<textarea>**, **<listbox>**, **<checkbox>**, **<checkboxes>**, **<radiobuttons>**, **<reset>**,   **<secret>**, **<secret>**+**<confirmation>**, **<upload>** +  * create quickly a CRUD controller, by using the jControllerDaoCrud controller (only indicate a jforms file, a jdao file, and that's all !) 
-  * some others will be available soon:  **<htmleditor>** **<hidden>**...+  * and many other controls are availabled: **<textarea>**, **<listbox>**, **<checkbox>**, **<checkboxes>**, **<radiobuttons>**, **<reset>**,   **<secret>**, **<secret>**+confirmation, **<upload>** 
 +  * some others will be available soon in Jelix 1.1 :  **<htmleditor>** **<hidden>** **<captcha>** (already in 1.1a1pre) etc.
  
  

en/tutorials/simple-jforms-example.txt · Last modified: 2012/12/06 20:38 by laurent

Recent changes RSS feed Creative Commons License