Simplified Error Handling

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Simplified Error Handling

Post 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..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply