Page 1 of 1

Simplified Error Handling

Posted: Sat Jul 22, 2006 12:12 am
by Benjamin
Let's say I have a form. Each item in the form can have multiple errors. (too short, too long, invalid type, etc)..

I have a class that validates the posted data in the form..

Code: Select all

if ($this->iAgree != 'checked')
        {
            $this->errors[] = 'You must agree to the terms of service.';
        }
Now, when I redisplay the form because of an error, I want to highlight the background of the specific question that didn't validate. The form is called from a template file.

I thought about assigning each error an id.. like this..

Code: Select all

if ($this->iAgree != 'checked')
        {
            $this->errors[2] = 'You must agree to the terms of service.';
        }
In this case I could check to see if this error was set and highlight the question when I redisplay the form.

But if I do that, I'll lose some of the errors because a question might have more than 1 error at a time. ie too long and contains invalid characters.

So... do I have to create a 2nd variable to indicate which question to highlight?

I'm just wondering how others would do something like this.

The form would look something like this..

Code: Select all

<td <?php if (isset($this->errors[2])) echo 'class="redBackground"'; ?>><input type="text" name="something" value="<?php echo $this->something; ?>" /></td>
This might sound like a weird question, but I am putting some serious effort into reducing code. I'm cutting it everywhere I can..

Posted: Sat Jul 22, 2006 6:13 am
by Chris Corbyn
Store your errors in array (write a method like pushError($string, $type)) and just check if that particular error exists ( containsError($type) ). You can then just call those methods from your view component.