I have a form which validates fine using classes but i would like the fields that have the error to be highlighted a different color
At the moment I just get a list of errors but would like a field that failed validation changed color.
here is my code
validator class
[PHP]<?php
class Validator {
var $errors;
function Validator($validateThis)
{
$this->errors = array();
$this->validate($validateThis);
}
function validate($validateThis) {}
function setError($msg)
{
$this->errors[] = $msg;
}
function isValid()
{
if (count($this->errors) > 0) {
return FALSE;
} else {
return TRUE;
}
}
function fetch()
{
$error = each($this->errors);
if ($error) {
return $error['value'];
} else {
reset($this->errors);
return FALSE;
}
}
}
?>[/PHP]
[PHP]this is one of my validation classes
<?php
require_once 'validators/Validator.php';
class ValidatePostcode extends Validator {
function validate($postcode)
{
if(empty($postcode)) {
$this->setError('Postcode field empty');
}
else {
if(!preg_match("/^([Gg][Ii][Rr]0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))){0,1}[0-9][A-Za-z]{2})$/",$postcode)) {
$this->setError('Postcode in invalid format');
}
}
}
}
?>[/PHP]
this is how i create a new instance of the class with the input from the form
[PHP]$validators[]=new ValidatePostcode($_POST['postcode']);[/PHP]
This is how i used to change the color of the input field
[PHP]<input type="text" <?php error_bool($error, "postcode"); ?>[/PHP]
with this function
[PHP]function error_bool($error, $field) {
if($error[$field]) {
print("style=\"background-color:#E7EFFB\"");
}
else {
print("style=\"background-color:white\"");
}
}[/PHP]