Connecting Tech Pros Worldwide Help | Site Map

PHP form validation with highlighted error fields

Newbie
 
Join Date: Aug 2007
Posts: 8
#1: Mar 29 '08
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]
Member
 
Join Date: Jul 2006
Posts: 92
#2: Mar 29 '08

re: PHP form validation with highlighted error fields


Could you post the errors so we can have a look?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Mar 30 '08

re: PHP form validation with highlighted error fields


Do you want to highlight the text entry field. like you do changing the background color, or do you want to highlight the field description of the field-in-error?

In the latter case you'll have to pass the field description in the validator call and set a new array in your class that can hold that name until printed out along with the error message.

Ronald
Newbie
 
Join Date: Aug 2007
Posts: 8
#4: Mar 30 '08

re: PHP form validation with highlighted error fields


I would like the text box to change color so they can easily see which text boxes have the errors.

Basically, the form fields are white until the field fails validation then they are changed color. Once the field is validated it changes back to white.

If you need to see any more code I can post it.....also I have no errors....i just need to know how to change the color

Many thanks for your replies
Reply