Documentation


SEARCH

TABLE OF CONTENT

    1. Getting started 2. Basic concepts 3. Request dispatching 4. Context container 5. Dual MVC 6. Component model: 7. Security 8. Configuration 9. Session handling 10. I18n 11. Cache 12. Logging 13. Error handling 14. Advanced Topics 15. API reference

      Tutorials: Frequently Asqued Questions

      See also:


      The Hello World Pro

      Form components tutorial

      Table of Contents

      Goal

      The goal of this tutorial is to learn how to use input components such the inputbox and the combobox.
      The application that we are going to develop is an improvement of The Hello World Deluxe: This application will ask the user to input some information in order to show a customized 'Hello world' message.

      What we are going to do in this tutorial is:

      • To modify the helloWorld template by adding 4 components: an inputbox, a combobox, a button and a label (templates/helloWorld.tpl)
      • To modify the event handler associated to the helloWorld view (libs/eventhandlers/HelloWorldEventHandler.class.php)

      Placing input components within the template

      First thing to do will be to modify the template helloWorld.tpl by adding the following components:

      1. <html>
      2. <head>
      3.   <title>The hello world deluxe in Lion</title
      4. </head>
      5. <body>
      6. Please state your name:<br
      7. <comp:inputbox name="nameInputBox"/><br>
      8. Please select a salutation:<br>
      9. <comp:combobox name="salutationComboBox">
      10.   <comp:item value="mr."  text="Mr"/>
      11.   <comp:item value="ms."  text="Ms"/>
      12.   <comp:item value="mrs." text="Mrs"/>
      13.   <comp:item value="dr."  text="Dr"/>
      14. </comp:combobox><br>
      15. And finally click on button:<br>
      16. <comp:commandbutton name="sayHelloButton" caption="Say hello!"/><br>
      17.  
      18. <h1><comp:label name="helloLabel"/></h1>
      19. </body>
      20. </html>

      As we can see, we have declared 4 components: an inputbox (nameInputBox), a combobox (saludationComboBox), a commandbutton (sayHelloButton) and a label (helloLabel).
      What we expect is to see a customized 'Hello World!' message like 'Hello mr. Antuan' when click on the button.

      Modifying the event handler

      The following task to do is to adapt the event handle to:

      • Read user input from components
      • Build a customized 'Hello World!' message and show it

      To do that, we have to define a method to handle the button click event and perform those tasks:

      1. <?php
      2.  
      3. class HelloWorldEventHandler extends __EventHandler {
      4.  
      5.     /**
      6.      * Shows a customized hello message into the helloLabel
      7.      * by reading from input components:
      8.      *
      9.      */
      10.     public function sayHelloButton_click({
      11.         //get the name from the input component:    
      12.         $name $this->getComponent('nameInputBox')->
      13.                        getValue();
      14.  
      15.         //get the salutation from the combobox:                       
      16.         $salutation $this->getComponent('salutationComboBox')->
      17.                        getValue();
      18.  
      19.         //compose the customized hello message:                       
      20.         $hello_message 'Hello ' $salutation ' ' $name;                       
      21.  
      22.         //show the customized hello message within the label:
      23.         $label_component $this->getComponent('helloLabel')->
      24.                                   setText($hello_message);
      25.  
      26.     }
      27.  
      28. }

      As we can see in the listing above, we have just retrieve the value of the input components by calling the getValue method.
      All the input components inherit from the __InputComponent, exposing the getValue method to retrieve the current value of each one

      Analogous, all those kind of components exposes another method (setValue) to set a new value.

      Executing the Hello World Pro

      Now it's time to execute our application. Just type the following url: http://yourdomain/pathtoyourapplication/helloWorld.html

      The aspect of our application should be like the following figure once we have click on the button:

      And again: No need to refresh the entire page to show the hello message. Ajax + Json instead of, which is handled automagically by the framework.