PHP Classes
elePHPant
Icontem

File: test_form.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of ferdhie  >  ooform  >  test_form.php  >  Download  
File: test_form.php
Role: Unit test script
Content type: text/plain
Description: test for ooform
Class: ooform
create, render, and validate html forms
Author: By
Last change:
Date: 12 years ago
Size: 3,278 bytes
 

Contents

Class file image Download
<?php

 
//
  // a test page for the ooform class
  //
  // Created by ferdhie <ferdhie@telkom.net>
  //

  // the ooforms class
 
require("class_form.inc");
 
 
// html headers
 
print ('<html>
  <head>
  <title>OO Forms Test Page</title>
  </head>
  <body>
  <pre>'
);
 
 
/**
  * A custom validation class inherited from Class CustomValidation
  *
  * Checks if the value not empty
  *
  * @author Herdian ferdianto <ferdhie@telkom.net>
  *
  */
 
class checkName extends CustomValidation {
   
/**
    * Constructor of the class
    *
    * Initialize the variables, and validate the parameters
    *
    * @param $data string value to validate
    * @return void
    * @access public
    *
    */
    
function checkName($data="") {
         
$this->errorMsg="Field name must not empty";
          if (empty(
$data)) {
              
$this->result=false;
               }
          else {
              
$this->result=true;
            }
       }
  }

 
/**
  * A custom validation class inherited from Class CustomValidation
  *
  * validate an email address
  *
  * @author Herdian ferdianto <ferdhie@telkom.net>
  *
  */
 
class checkEmail extends CustomValidation {
 
   
/**
    * Constructor of the class
    *
    * Initialize the variables, and validate the parameters
    *
    * @param $data string value to validate
    * @return void
    * @access public
    *
    */
   
function checkEmail($data="") {
     
$this->errorMsg="Email fake!";
      if (empty(
$data)) {
       
$this->result=false;
        }
      else {
        if (
ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $data,$regs)) {
         
$this->result=true;
        }
        else {
         
$this->result=false;
          }
      }
    }
  }

 
// first build the ooforms object
 
$f = new ooform('formGuest','method="POST" action="'.$HTTP_SERVER_VARS['PHP_SELF'].'"');

 
// create some forms elements
 
$f->add_elements(new textbox($f,'inputName','','size="30"',true));
 
$f->add_elements(new textbox($f,'inputEmail','','size="30"',true));
 
$f->add_elements(new textarea($f,'inputBody','','cols="30" rows="8"',true));
 
$f->add_elements(new submit($f,'action','submit'));

 
// Initilize the validation object match with the fields,
  // and check if every fields is valid then echo the values
 
if ($f->inputName->validate("checkName") && $f->inputEmail->validate("checkEmail") && $f->inputBody->validate("checkName")) {
    print (
"\$f->inputName = $f->inputName->value\n");
    print (
"\$f->inputEmail = $f->inputName->value\n");
    print (
"\$f->inputMessage = $f->inputName->value\n");
    }
 
// if not, echoes the forms
  // and echoes the error messages iof any.
 
else {
   
$strForm = $f->start()."\n".
   
"Name\t".$f->inputName->render()."\t".$f->inputName->showError()."\n\n".
   
"Email\t".$f->inputEmail->render()."\t".$f->inputEmail->showError()."\n\n".
   
"Message\t".$f->inputBody->render()."\t".$f->inputBody->showError()."\n\n".
   
$f->action->render()."\n".
   
$f->end();
 
    print (
$strForm);
    }
   
 
// footers
 
print ("</pre>\n
  </body>\n
  </html>"
);
?>