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($vali dateThis)
{
$this->errors = array();
$this->validate($vali dateThis);
}
function validate($valid ateThis) {}
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 ValidatePostcod e extends Validator {
function validate($postc ode)
{
if(empty($postc ode)) {
$this->setError('Post code 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('Post code in invalid format');
}
}
}
}
?>[/PHP]
this is how i create a new instance of the class with the input from the form
[PHP]$validators[]=new ValidatePostcod e($_POST['postcode']);[/PHP]
This is how i used to change the color of the input field
[PHP]<input type="text" <?php error_bool($err or, "postcode") ; ?>[/PHP]
with this function
[PHP]function error_bool($err or, $field) {
if($error[$field]) {
print("style=\" background-color:#E7EFFB\" ");
}
else {
print("style=\" background-color:white\"") ;
}
}[/PHP]
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($vali dateThis)
{
$this->errors = array();
$this->validate($vali dateThis);
}
function validate($valid ateThis) {}
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 ValidatePostcod e extends Validator {
function validate($postc ode)
{
if(empty($postc ode)) {
$this->setError('Post code 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('Post code in invalid format');
}
}
}
}
?>[/PHP]
this is how i create a new instance of the class with the input from the form
[PHP]$validators[]=new ValidatePostcod e($_POST['postcode']);[/PHP]
This is how i used to change the color of the input field
[PHP]<input type="text" <?php error_bool($err or, "postcode") ; ?>[/PHP]
with this function
[PHP]function error_bool($err or, $field) {
if($error[$field]) {
print("style=\" background-color:#E7EFFB\" ");
}
else {
print("style=\" background-color:white\"") ;
}
}[/PHP]
Comment