SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
Form Validation NG
João Prado Maia
Lead Software Developer, Alert Logic



8/2/2007
Introduction
• Background
   – Form validation is always a pain to
     implement
       • Too many moving parts
       • Not always standardized
   – Built prototype in 2006
       • Used JavaScript/AJAX for client-side
         validation
       • Not complete when doing fallback
         server-side validation
       • Required lots of small changes to forms




                             Slide: 2
Goals
• Validation both on client and server sides
   – Avoid form/HTTP-request spoofing
   – Validation rules are generated from the same
     centralized source
   – No duplication of validation code
• Works well with pre-existing Smarty templates
   – Minimum amount of changes required on each form
   – Not enough time to re-architect a complete data-model
     form generation code
• Easy to understand code



                           Slide: 3      Source: IP Development Network. Understanding Your Business Customers
Final look and feel




               Slide: 4   Source: Strategy Analytics
Overview of client-side
{validation}
<form name=quot;test_formquot; ... >
...
</form>
{/validation}


Simple steps:
1.    Add extra {validation} tags around the form
2.    ???
3.    Profit!


                         Slide: 5
But wait, there’s more!
•       Not really, the {validation}
        function is all you need
    –      Done as a Smarty block function
    –      Parses XHTML content of form
    –      Sets up proper onBlur/onChange
           event handlers to re-validate
           error’ed-out forms
    –      Sets up onSubmit to process form
           validation




                             Slide: 6
Diagram of client-side




              Slide: 7
Diagram of client-side (2)




               Slide: 8
It’s all in the XML
• Both client and server sides use XML rules to
  validate the form input
   – One XML file per template
   – Easy to define validation rules per form and form field
   – Multiple validation rules per form fields also possible
• Stores error messages as well
   – Easy to extend into i18n error messages
• Adding a new validation rule is as easy as
  implementing it in PHP – client-side comes for
  free



                            Slide: 9
Example XML file
<?xml version=quot;1.0quot; encoding=quot;iso-8859-1quot;?>
<!DOCTYPE form_validation SYSTEM quot;form_validation.dtdquot;>
<form_validation>
    <form name=quot;case_creation_formquot;>
        <field stop_on_errors=“true” name=“user_emailquot;>
            <validation type=quot;non_emptyquot;>
                <error_msg>Enter your email address to be notified about this case.</error_msg>
            </validation>
            <validation type=quot;e-mailquot;>
                <error_msg>Summary must be a valid e-mail address.</error_msg>
            </validation>
        </field>
        <field name=quot;descriptionquot;>
            <validation type=quot;non_emptyquot;>
                <error_msg>Enter a description for this case.</error_msg>
            </validation>
        </field>
        <field name=quot;priorityquot;>
            <validation type=quot;non_emptyquot;>
                <error_msg>Define a priority for this case.</error_msg>
            </validation>
        </field>
    </form>
</form_validation>




                                          Slide: 10
Overview of server-side
• Small changes needed
• Wrap form handling code with validation routine

<?php
$validation = new Form_Validation('template_name', 'form_name');
if ($validation->isValid()) {
    /* Process form submission */
}
?>


• No need to handle errors, just display the template
  again with the {validation} function



                              Slide: 11
Diagram of server-side




            Slide: 12
Smarty magic




           Slide: 13
Smarty magic (2)
• Smarty/plugins/block.validation.php
<?php
function smarty_block_validation($params, $content, &$smarty)
{
    // ...

     $doc = new DOMDocument();
     @$doc->loadHTML($content);

     foreach ($doc->getElementsByTagName('form') as $form) {
         $form_name = $form->getAttribute('name');
         $form->setAttribute('onsubmit',
                 'javascript: return validateForm(t_name, this.name)');
     }

     if (!empty($errors)) {
         $div = $doc->createElement(quot;DIVquot;);
         $div->setAttribute('id', 'header_error_validation');
         // ...
     }

     // include <script> tags
     // ...

     return $changed_html;
}
?>




                                           Slide: 14
Potential improvements
• Change the Form_Validation class to
  use the DOM extension
  – currently using SimpleXML
  – only 1 required extension, instead of 2




                   Slide: 15
Features
• Works with or without JavaScript
  enabled on browser
• Validation code is centralized – no more
  duplication
• Dead-simple to implement




                  Slide: 16
Last slide
PHP extensions
    JSON
           http://us3.php.net/manual/en/ref.json.php
    SimpleXML
           http://us3.php.net/manual/en/ref.simplexml.php
    DOM
           http://us3.php.net/manual/en/ref.dom.php

Other tools
    Smarty
           http://smarty.php.net
    Smarty – Block functions
           http://smarty.php.net/manual/en/plugins.block.functions.php
    Smarty PHP Template Programming and Applications
           http://smartybook.com




                                             Slide: 17

Mais conteúdo relacionado

Semelhante a Form Validation NG

Spring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGSpring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGTed Pennings
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingRichard Clark
 
BITM3730Week11.pptx
BITM3730Week11.pptxBITM3730Week11.pptx
BITM3730Week11.pptxMattMarino13
 
Web internship Yii Framework
Web internship  Yii FrameworkWeb internship  Yii Framework
Web internship Yii FrameworkNoveo
 
Zembly Programming Language
Zembly Programming LanguageZembly Programming Language
Zembly Programming Languagezembly
 
CICONF 2012 - Don't Make Me Read Your Mind
CICONF 2012 - Don't Make Me Read Your MindCICONF 2012 - Don't Make Me Read Your Mind
CICONF 2012 - Don't Make Me Read Your Mindciconf
 
Continously delivering
Continously deliveringContinously delivering
Continously deliveringJames Cowie
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4Javier Eguiluz
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMDean Hamstead
 
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Damien Carbery
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Codescidept
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin GeneratorJohn Cleveley
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworksguestf7bc30
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 

Semelhante a Form Validation NG (20)

Spring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGSpring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUG
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
 
BITM3730Week11.pptx
BITM3730Week11.pptxBITM3730Week11.pptx
BITM3730Week11.pptx
 
Web internship Yii Framework
Web internship  Yii FrameworkWeb internship  Yii Framework
Web internship Yii Framework
 
Zembly Programming Language
Zembly Programming LanguageZembly Programming Language
Zembly Programming Language
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
 
Dancing with websocket
Dancing with websocketDancing with websocket
Dancing with websocket
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
mean stack
mean stackmean stack
mean stack
 
CICONF 2012 - Don't Make Me Read Your Mind
CICONF 2012 - Don't Make Me Read Your MindCICONF 2012 - Don't Make Me Read Your Mind
CICONF 2012 - Don't Make Me Read Your Mind
 
Continously delivering
Continously deliveringContinously delivering
Continously delivering
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PM
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
 
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworks
 
Java script
Java scriptJava script
Java script
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 

Mais de joaopmaia

AFNetworking
AFNetworking AFNetworking
AFNetworking joaopmaia
 
Core Data presentation
Core Data presentationCore Data presentation
Core Data presentationjoaopmaia
 
SQLite Techniques
SQLite TechniquesSQLite Techniques
SQLite Techniquesjoaopmaia
 
SQLite Techniques
SQLite TechniquesSQLite Techniques
SQLite Techniquesjoaopmaia
 
Meetup uikit programming
Meetup uikit programmingMeetup uikit programming
Meetup uikit programmingjoaopmaia
 
Web App Testing With Selenium
Web App Testing With SeleniumWeb App Testing With Selenium
Web App Testing With Seleniumjoaopmaia
 
Eventum Presentation
Eventum PresentationEventum Presentation
Eventum Presentationjoaopmaia
 

Mais de joaopmaia (7)

AFNetworking
AFNetworking AFNetworking
AFNetworking
 
Core Data presentation
Core Data presentationCore Data presentation
Core Data presentation
 
SQLite Techniques
SQLite TechniquesSQLite Techniques
SQLite Techniques
 
SQLite Techniques
SQLite TechniquesSQLite Techniques
SQLite Techniques
 
Meetup uikit programming
Meetup uikit programmingMeetup uikit programming
Meetup uikit programming
 
Web App Testing With Selenium
Web App Testing With SeleniumWeb App Testing With Selenium
Web App Testing With Selenium
 
Eventum Presentation
Eventum PresentationEventum Presentation
Eventum Presentation
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Form Validation NG

  • 1. Form Validation NG João Prado Maia Lead Software Developer, Alert Logic 8/2/2007
  • 2. Introduction • Background – Form validation is always a pain to implement • Too many moving parts • Not always standardized – Built prototype in 2006 • Used JavaScript/AJAX for client-side validation • Not complete when doing fallback server-side validation • Required lots of small changes to forms Slide: 2
  • 3. Goals • Validation both on client and server sides – Avoid form/HTTP-request spoofing – Validation rules are generated from the same centralized source – No duplication of validation code • Works well with pre-existing Smarty templates – Minimum amount of changes required on each form – Not enough time to re-architect a complete data-model form generation code • Easy to understand code Slide: 3 Source: IP Development Network. Understanding Your Business Customers
  • 4. Final look and feel Slide: 4 Source: Strategy Analytics
  • 5. Overview of client-side {validation} <form name=quot;test_formquot; ... > ... </form> {/validation} Simple steps: 1. Add extra {validation} tags around the form 2. ??? 3. Profit! Slide: 5
  • 6. But wait, there’s more! • Not really, the {validation} function is all you need – Done as a Smarty block function – Parses XHTML content of form – Sets up proper onBlur/onChange event handlers to re-validate error’ed-out forms – Sets up onSubmit to process form validation Slide: 6
  • 9. It’s all in the XML • Both client and server sides use XML rules to validate the form input – One XML file per template – Easy to define validation rules per form and form field – Multiple validation rules per form fields also possible • Stores error messages as well – Easy to extend into i18n error messages • Adding a new validation rule is as easy as implementing it in PHP – client-side comes for free Slide: 9
  • 10. Example XML file <?xml version=quot;1.0quot; encoding=quot;iso-8859-1quot;?> <!DOCTYPE form_validation SYSTEM quot;form_validation.dtdquot;> <form_validation> <form name=quot;case_creation_formquot;> <field stop_on_errors=“true” name=“user_emailquot;> <validation type=quot;non_emptyquot;> <error_msg>Enter your email address to be notified about this case.</error_msg> </validation> <validation type=quot;e-mailquot;> <error_msg>Summary must be a valid e-mail address.</error_msg> </validation> </field> <field name=quot;descriptionquot;> <validation type=quot;non_emptyquot;> <error_msg>Enter a description for this case.</error_msg> </validation> </field> <field name=quot;priorityquot;> <validation type=quot;non_emptyquot;> <error_msg>Define a priority for this case.</error_msg> </validation> </field> </form> </form_validation> Slide: 10
  • 11. Overview of server-side • Small changes needed • Wrap form handling code with validation routine <?php $validation = new Form_Validation('template_name', 'form_name'); if ($validation->isValid()) { /* Process form submission */ } ?> • No need to handle errors, just display the template again with the {validation} function Slide: 11
  • 13. Smarty magic Slide: 13
  • 14. Smarty magic (2) • Smarty/plugins/block.validation.php <?php function smarty_block_validation($params, $content, &$smarty) { // ... $doc = new DOMDocument(); @$doc->loadHTML($content); foreach ($doc->getElementsByTagName('form') as $form) { $form_name = $form->getAttribute('name'); $form->setAttribute('onsubmit', 'javascript: return validateForm(t_name, this.name)'); } if (!empty($errors)) { $div = $doc->createElement(quot;DIVquot;); $div->setAttribute('id', 'header_error_validation'); // ... } // include <script> tags // ... return $changed_html; } ?> Slide: 14
  • 15. Potential improvements • Change the Form_Validation class to use the DOM extension – currently using SimpleXML – only 1 required extension, instead of 2 Slide: 15
  • 16. Features • Works with or without JavaScript enabled on browser • Validation code is centralized – no more duplication • Dead-simple to implement Slide: 16
  • 17. Last slide PHP extensions JSON http://us3.php.net/manual/en/ref.json.php SimpleXML http://us3.php.net/manual/en/ref.simplexml.php DOM http://us3.php.net/manual/en/ref.dom.php Other tools Smarty http://smarty.php.net Smarty – Block functions http://smarty.php.net/manual/en/plugins.block.functions.php Smarty PHP Template Programming and Applications http://smartybook.com Slide: 17