I wanted an easy way to validate my forms in php. I didn't want to write tons of if loops and stuff each page since I have a ton of forms and stuff on the site I am making. So I decided to make a class based approach.
What I am really looking for is just feedback on the method that I've used. See if there was a better way to do things. Right now it registers a rule into an array and then the check function goes through all the rules to see if the form is valid or not, and then the errors are output if the validation fails.
class FormCheck
{
public $checked;
public $register;
public $validate;
public $header;
public $footer;
/*
*@param $prepend - a string that is added to the front of error outputs on the page
*@param $appdend - a string that is added to the end of error outputs on the page
*/
public function __construct($prepend = "", $append = "")
{
$this->checked = false;
$this->register = array();
$this->validate = true;
$this->header = $prepend;
$this->footer = $append;
}
/*
* Adds a rule to a list that is later used by the check function to validate the form.
*
* @param $ruleId - The actual name used to refer to the rule. Is used later on by the error function.
* @param $fieldname - The name given to the field in the form (IE: <input name="fieldname" type="text">)
* @param $rule - The type of check that will be performed.
*/
public function register($ruleId, $fieldname, $rule, $args = "")
{
$row = array('name' => $fieldname, 'check' => $rule, 'args' => $args, 'error' => "noCheck");
$this->register[$ruleId] = $row;
}
/*
* Runs a check on the form by going through each rule created by the register function.
* To add a new type of check you must add a line to the switch statement
* case "checkName": $this->methodname($c);
*
* @return A boolean representing if the form passed validation or not.
*/
public function check()
{
$this->validate = true;
foreach($this->register as $c => $value)
{
switch($this->register[$c]['check'])
{
case "noNull": $this->noNull($c); break;
case "mustMatch": $this->mustMatch($c); break;
case "minValue": $this->minValue($c, $this->register[$c]['args']); break;
case "isEmail": $this->isEmail($c); break;
}
}
$this->checked = true;
}
//Prints the error.
/*
* Displays the appropriate error when the specific rule fails validation
*
* @param $ruleId - The id given to the rule when it was created with the register function
*
* @return - Will output an error created by one of the Error checking functions only if rule failed validation.
*/
public function error($ruleId)
{
if ($this->checked == true && $this->register[$ruleId][error] != "validated")
{
echo $this->header . $this->register[$ruleId]['error'] . $this->footer;
}
}
//////////////////
//Error checking//
//////////////////
/*
* Checks the field to ensure that it isn't null.
*
* @param $ruleId The id of the rule defined at the register function.
*/
public function noNull($ruleId)
{
$temp = $this->register[$ruleId]['name'];
if (empty($_POST[$temp]))
{
$this->validate = false;
$this->register[$ruleId]['error'] = "This field cannot be empty.";
}
else
{
$this->register[$ruleId]['error'] = "validated";
}
}
public function mustMatch($ruleId)
{
$temp = $this->register[$ruleId]['name'];
if ($_POST[$temp] == $_POST[$temp . "check"])
{
$this->register[$ruleId]['error'] = "validated";
}
else
{
$this->validate = false;
$this->register[$ruleId]['error'] = "The two fields did not match.";
}
}
public function minValue($ruleId, $size)
{
if (strlen($_POST[$this->register[$ruleId]['name']]) < $size)
{
$this->validate = false;
$this->register[$ruleId]['error'] = "This field must contain at least {$size} characters.";
}
else
{
$this->register[$ruleId]['error'] = "validated";
}
}
public function isEmail($ruleId)
{
if (ereg("[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+", $_POST[$this->register[$ruleId]['name']]))
{
$this->register[$ruleId]['error'] = "validated";
}
else
{
$this->validate = false;
$this->register[$ruleId]['error'] = "You did not supply a valid e-mail adress.";
}
}
}
?>
and here is the form
<?php
$form = new formcheck("", "<br>");
//Process the form!
if ($_POST['submit'] == "submit")
{
//Describe page.
$form->register("username_min", "username", "minValue", "3");
$form->register("password_min", "password", "minValue", "6");
$form->register("password_mustMatch", "password", "mustMatch");
$form->register("email", "email", "isEmail");
$form->register("email_mustMatch", "email", "mustMatch");
//Process the form.
$form->check();
//print_r($form->register);
//If valid enter info and exit
if ($form->validate == true)
{
echo "omg the form validated";
new error("");
}
//Else display the page again.
}
//Display form!
?>
<form action="<?php echo $PHP_SELF ?>" method="post">
<table border="1" cellpadding="5">
<tr><td colspan="2">Registration Information (all fields are required)</td></tr>
<tr><td>Username:</td><td><?php $form->error("username_min") ?><input type="text" size="20" name="username" maxlength="15"></td></tr>
<tr><td>Password:</td><td><?php $form->error("password_min") ?><input type="password" size="20" name="password" maxlength="20"></td></tr>
<tr><td>Enter Password Again:</td><td><?php $form->error("password_mustMatch") ?><input type="password" size="20" name="passwordcheck" maxlength="20"></td></tr>
<tr><td>E-mail:</td><td><?php $form->error("email") ?><input type="text" size="20" name="email" maxlength="30"></td></tr>
<tr><td>Enter E-mail Again:</td><td><?php $form->error("email_mustMatch") ?><input type="text" size="20" name="emailcheck" maxlength="30"></td></tr>
<tr><td>Show my e-mail to others?:</td><td>Yes <input type="radio" checked="checked" name="showemail" value="y">No <input type="radio" value="y" name="showemail"></td></tr>