Jump to content

Php class tutorial


jcleaver310

Recommended Posts

I followed the tutorial at http://www.devshed.com/c/a/PHP/Building-An-Extensible-Form-Validator-Class/ because I wanted to have an easier way to build and maintain form validation.

I completed the tutorial, but the form does not register the form as being filled out.

Here is the class code (FormValidator.class.inc)

<?php

//FormValidator.class.inc

//class to perform form validation

class FormValidator {

 

//---------------private variables------------------

 

var $_errorList;

//the _ prefix is used to distinguish private variables from public variables

 

//---------------methods (private)------------------

 

//functio to get the value of a variable (field)

function _getValue($field) {

global ${$field}; //makes the field entry a global variable

return ${$field};

}

 

//---------------methods (public)-------------------

 

//it is a good idea to run resetErrorList() whenever the class is first initialized

//PHP makes it possible to automatically execute a specific function when a new instance of a class is spawned. This function is referred to as a "constructor" and must have the same name as the class.

//reset error list

function FormValidator() {

$this->resetErrorList();

}

 

 

//begin basic validation routines

 

//check whether input is empty

//the isEmpty public method is called with 2 arguments, one for the form variable and one for the error message

//the method internally calls the _getValue method and then trim()s the result to check if it is empty

// if the variable is empty, a new element is added class variable $_errorList (an array) to represent the error. This new element is itself a three-element associative array, with the keys "field", "value" and "msg", representing the form variable name, the current value of that variable, and the corresponding error message

// if the variable is not empty, it will pass the test and return true

// this function, along with _getValue, allows the user to get both the field name and the field value

//the $this prefix provides a convenient way to access variables and functions which are "local" to the class.

function isEmpty($field, $msg) {

$value = $this->_getValue($field);

if (trim($value) == "") {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether input is a string

function isString($field, $msg) {

$value = $this->_getValue($field);

if(!is_string($value)) {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether input is a number

function isNumber($field, $msg) {

$value = $this->_getValue($field);

if(!is_numeric($value)) {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether input is an integer

function isInteger($field, $msg) {

$value = $this->_getValue($field);

if(!is_integer($value)) {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether input is a float

function isFloat($field, $msg) {

$value = $this->_getValue($field);

if(!is_float($value)) {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether input is within a valid numeric range

function isWithinRange($field, $msg, $min, $max) {

$value = $this->_getValue($field);

if(!is_numeric($value) || $value < $min || $value > $max) {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

else {

return true;

}

}

 

//check whether the input is alphanumeric

function isAlpha($field, $msg) {

$value = $this->_getValue($field);

$pattern = "/^[a-zA-Z]+$/";

if (preg_match($pattern, $value)) {

return true;

}

else {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

}

 

//check whether input is a valid email address

function isEmailAddress($field, $msg) {

$value = $this->_getValue($field);

$pattern =

"/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/

";

if (preg_match($pattern, $value)) {

return true;

}

else {

$this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);

return false;

}

}

 

//end basic validation routines-----

 

//return the list of current errors

function getErrorList() {

return $this->_errorList;

}

 

//check whether any errors have occured in validation

//this method checks the size of the $_errorList array; if the size is greater than one, it implies that one or more errors have occured during validation

//it returns a Boolean value

function isError() {

if(sizeof($this->_errorList) > 0) {

return true;

}

else {

return false;

}

}

 

 

//reset the error list to a blank array

function resetErrorList() {

$this->_errorList = array();

}

 

}//end FormValidator class

?>

-------Here is the form (simpleform.html)

<html>

<head>

<basefont face="Arial">

</head>

<body>

<form action="processor.php" method="POST">

<b>Name:</b>

<br>

<input type="text" name="name" size="15">

 

<p>

 

<b>Age:</b>

<br>

<input type="text" name="age" size="2" maxlength="2">

 

<p>

 

<b>Sex:</b>

<br>

<input type="Radio" name="sex" value="m">Male

<input type="Radio" name="sex" value="f">Female

 

<p>

 

<b>Favourite sandwich type:</b>

<br>

<select name="stype">

<option value="">-select one-</option>

<option value="1">Thin crust</option>

<option value="2">Thick crust</option>

<option value="3">Toasted</option>

</select>

 

<p>

 

<b>Favourite sandwich filling:</b>

<br>

<input type="Checkbox" name="sfill[]" value="BLT">Bacon, lettuce tomato

<input type="Checkbox" name="sfill[]" value="EC">Egg and cheese

<input type="Checkbox" name="sfill[]" value="PBJ">Peanut butter and jelly

 

<p>

 

<input type="Submit" name="submit" value="Save">

 

</form>

</body>

</html>

------and here is processor.php

<?php

//include class

include("FormValidator.class.inc");

// instantiate object

$fv = new FormValidator();

//perform validation

$fv->isEmpty("name", "Please enter a name");

$fv->isNumber("age", "Please enter a valid age");

$fv->isWithinRange("age", "Please enter an age within the numeric range 1-99", 1, 99);

$fv->isEmpty("sex", "Please enter your sex");

$fv->isEmpty("stype", "Please select one of the listed sandwich types");

$fv->isEmpty("sfill", "Please select one or more of the listed sandwich fillings");

if($fv->isError()) {

$errors = $fv->getErrorList();

echo "<b>The operation could not be performed because one or more error(s) occurred.</b> <p> Please resubmit the form after making the following changes:";

echo "<ul>";

foreach($errors as $e) {

echo "<li>" . $e['msg'] . "</li>";

}

echo "</ul>";

}

else {

//do something useful with the data

echo "Data OK";

}

 

?>

Link to comment
Share on other sites

That code is so out of date it's not funny. As of next month (April) that code is 10 years out of date. In addition to using bad coding - global and variable variables to access main program variables, it relies on register_globals to set those main program variables from the form data.

 

That class code WON'T work as written.

 

Edit: and in fact, the two other php form classes that the author mentions to use instead of his own simple/first attempt are equally out of date and won't work as written.

 

Edit2: in fact that code was developed/tested using php 4.1.1.

Link to comment
Share on other sites

The most straightforward way of getting the class to work with the least amount of changes would be to pass an array of the form data ($_POST) into the class when you make an instance of the class, then change the _getValue() method to reference that copy of the form data.

Link to comment
Share on other sites

Next problem with the class code and the tutorial is that it does not address arrays of values, so the 'sfill[]' checkboxes will always be considered empty in the code as written. Since I don't think the trim() function ever operated on an array, the tutorial code probably never worked for checkboxes.

 

You would either need to add specific methods for fields that are arrays or alter the logic to detect between scaler variables and arrays and take an appropriate action for both possibilities.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.