main | downloads | documentation | comunity | about lionframework

The Hello World Enterprise Edition

Validating user inputs

Table of Contents

Goal

The goal of this tutorial is to learn how to use validator components.
The application that we are going to develop is a single page with a typical registration form. This page will contain some validator components in order to check user inputs before submitting.

What we are going to do in this tutorial is:

  • To create a page with a registration form.
  • To add validators associated to input components.
  • To create an event handler in order to perform some server-side validations as well.

Create a new controller

Let's create a new controller as following:

  1. <?php
  2.  
  3. class RegistrationController extends __ActionController {
  4.     
  5.     public function defaultAction({
  6.         return new __ModelAndView('registrationForm');
  7.     }
  8.     
  9. }

This controller just returns a __ModelAndView instance to execute the registrationForm view.

Create a new template

Now will create a template containing the registration form as following:

  1. <html>
  2. <head>
  3.   <title>Registration form</title
  4. </head>
  5. <body>
  6. <h1>Registration form</h1>
  7.  
  8. <comp:form name="registration_form" action="register">
  9.  
  10.   First name:<br>
  11.       <comp:inputbox name="first_name"/><br><br>
  12.   Last name:<br
  13.       <comp:inputbox name="last_name"/><br><br>
  14.   Birdth date
  15.       <comp:datebox name="birdth_date"/> (YYYY-mm-dd)<br><br>
  16.   Sex:<br
  17.       <comp:optionbox group="sex" name="male" caption="Male">
  18.       &nbsp;
  19.       <comp:optionbox group="sex" name="female" caption="Female"><br><br>
  20.   Email address:<br>
  21.       <comp:inputbox name="email"/><br><br>
  22.   Password:<br
  23.       <comp:inputbox type="password" name="password"/><br><br>
  24.   Repeat password:<br
  25.       <comp:inputbox type="password" name="password_confirmation"/><br><br>
  26.   Question:<br>
  27.       <comp:combobox name="question">
  28.           <comp:item text="Your favorite pet?" value="pet"/>
  29.           <comp:item text="Your first car?" value="car"/>
  30.       </comp:combobox/><br><br>
  31.   Answer:<br>
  32.       <comp:inputbox name="answer"/><br><br>
  33.   <comp:commandbutton name="send" caption="Send"/>
  34.  
  35. </comp:form>  
  36.  
  37. </body>
  38. </html>

As you can see, we have just defined a typical registration form asking for the following values: the first name, the last name, the birdth date, the sex, an email address, a password and also a reminder question plus the answer.

Now it's time to define validation rules for our input components

Defining validation rules

There is an special component to automate a great part of the validation process: The validation rule component

This component uses a great javascript library to perform client-side validations: The LiveValidation library, by Alec Hill.
This library is plenty mature and covers a great number of different validation cases, but may be one of the most notable feature the capability to validate as soon as values are written within the form.

Continuing with the validation rule component, it has 2 main tasks:

  • To generate optimized javascript code on client side
  • To intercept requests that require parameter validation in order to perform same validations on server-side
So, once we have defined a validation rule, by default it performs the same validations on both the client and server side (double validation)

Not let's define some validation rules for input components within our registration form:

  1. <html>
  2. <head>
  3.   <title>Registration form</title
  4. </head>
  5. <body>
  6. <h1>Registration form</h1>
  7. <comp:form name="registration_form" action="register">
  8.  
  9.   First name:<br>
  10.       <comp:inputbox name="first_name"/><br><br>
  11.   Last name:<br
  12.       <comp:inputbox name="last_name"/><br><br>
  13.   Birdth date
  14.       <comp:datebox name="birdth_date"/> (YYYY-mm-dd)<br><br>
  15.   Sex:<br
  16.       <comp:optionbox group="sex" name="male" caption="Male">
  17.       &nbsp;
  18.       <comp:optionbox group="sex" name="female" caption="Female"><br><br>
  19.   Email address:<br>
  20.       <comp:inputbox name="email"/><br><br>
  21.   Password:<br
  22.       <comp:inputbox type="password" name="password"/><br><br>
  23.   Repeat password:<br
  24.       <comp:inputbox type="password" name="password_confirmation"/><br><br>
  25.   Question:<br>
  26.       <comp:combobox name="question">
  27.           <comp:item text="Your favorite pet?" value="pet"/>
  28.           <comp:item text="Your first car?" value="car"/>
  29.       </comp:combobox/><br><br>
  30.   Answer:<br>
  31.       <comp:inputbox name="answer"/><br><br>
  32.   <comp:commandbutton name="send" caption="Send"/>
  33.  
  34. </comp:form>  
  35.   
  36. <!--/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */-->
  37. <!--/*        Validation rules         */-->  
  38. <!--/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */-->
  39.  
  40. <comp:validationrule component="first_name" 
  41.                      mandatory="true" 
  42.                      maxLength="100"
  43.                        pattern="^[A-Za-z\s]+$"/>
  44.  
  45. <comp:validationrule component="last_name" 
  46.                      maxLength="100"
  47.                        pattern="^[A-Za-z\s]+$"/>
  48.                   
  49. <comp:validationrule component="birdth_date" 
  50.                      mandatory="true" 
  51.                        pattern="^\d\d\d\d\-\d\d-\d\d$"/>
  52.  
  53. <comp:validationrule component="email" 
  54.                      mandatory="true" 
  55.                      maxLength="150"
  56.                         format="email"/>
  57.                    
  58. <comp:validationrule component="password" 
  59.                      mandatory="true" 
  60.                      minLength="6"
  61.                      maxLength="15"/>
  62.  
  63. <comp:validationrule component="password_confirmation" 
  64.                      mandatory="true"
  65.                 matchComponent="password"/>
  66.                      
  67. <comp:validationrule component="answer" 
  68.                      maxLength="100"
  69.                      mandatory="true"/>                     
  70.                    
  71. </body>
  72. </html>

As you can see, we have just defined a typical registration form asking for the following values: the first name, the last name, the birdth date, the sex, an email address, a password and also a reminder question plus the answer.

Now it's time to define validation rules for our input components

Executing the Hello World Enterprise Edition

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

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.

Reference Manual

Table of content

    Getting started Request dispatching Context container MVC Component model: Security Configuration Session handling I18n Cache Logging Error handling API reference

      Tutorials: Frequently Asqued Questions

      See also:
      Copyright (c) 2008, www.lionframework.org