Properly storing error messages and displaying them with OOP I have a class that I am using to initialize a form for editing. Once the form is submitted, data validation is done through the setters of my class. Good or bad input will be set in order to re-display them in the form (is this bad?). Everything works fine, but I am not too happy with how I am storing and displaying the error messages.

Any suggestions on how I can improve?

Course.class.php

&lt;?php
class Course {
    protected $_code;
    protected $_name;
    protected $_errors = array();

    public function __constructor($id) {
        if (is_int($id)) {
            $this-&gt;_load($id);
        }            
    }

    public function getCode() { return $this-&gt;_code; }

    public function setCode($value) { 
        $this-&gt;_code = $value;
        if (empty($value)) {
            $this-&gt;setError('code', 'Enter a code.');
        } else if (strlen($value) &gt; 10) {
            $this-&gt;setError('code', 'The code you provided must be less than 10 characters.');
        }
    }

    public function getName() { return $this-&gt;_name; }

    public function setName($value) { 
        $this-&gt;_name = $value;
        if (empty($value)) {
            $this-&gt;setError('name', 'Enter a name.');
        } else if (strlen($value) &gt; 50) {
            $this-&gt;setError('name', 'The name you provided must be less than 50 characters.');
        }
    }

    public function getError($name) {
        return isset($this-&gt;_errors[$name]) ? $this-&gt;_errors[$name] : '';
    }

    public function setError($name, $value) {
        $this-&gt;_errors[$name] = $value;
    }

    public function hasErrors() {
        return !empty($this-&gt;_errors);
    }

    public function _load($id) {
        // load record from database
    }

    public function save() {
        if (!this-&gt;hasErrors()) {
            // save record to database
        }
    }     
}
?&gt;


I have looked around and many have suggest to use exceptions. However, I don't quite get how I can use them because wouldn't throwing an exception in one setter cause the subsequent setters from not being called.


OOP Design Question - Validating properties
Validate on entry or before saving?


Here is how I am currently using my class:

edit_course.php

&lt;?php
$filter = array(
    'hidSubmit' =&gt; FILTER_VALIDATE_BOOLEAN,
    'code' =&gt; FILTER_SANITIZE_STRING, 
    'name' =&gt; FILTER_SANITIZE_STRING
);

$c = filter_input(INPUT_GET, 'c', FILTER_VALIDATE_INT);
$course = new Course($c);

$inputs = filter_input_array(INPUT_POST, $filter);
if ($inputs['hidSubmit']) {
    $course-&gt;setName($inputs['name']);
    $course-&gt;setDescription($inputs['description']);
    $course-&gt;save();
    refreshPage();
}

require_once 'header_admin.php';
?&gt;


&lt;form id="editForm" name="editForm" action="" method="post"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Edit Course&lt;/legend&gt;
    &lt;label&gt;Code&lt;/label&gt;
    &lt;input type="text" name="code" value="&lt;?php echo $course-&gt;getCode(); ?&gt;" maxlength="10" size="8" /&gt;
    &lt;span class="error"&gt;&lt;?php echo $course-&gt;getError('code'); ?&gt;&lt;/span&gt;
    &lt;label&gt;Name&lt;/label&gt;
    &lt;input type="text" name="name" value="&lt;?php echo $course-&gt;getName(); ?&gt;" maxlength="45" size="25" /&gt;
    &lt;span class="error"&gt;&lt;?php echo $course-&gt;getError('name'); ?&gt;&lt;/span&gt;
    &lt;input type="submit" value="Save" /&gt;
    &lt;input type="hidden" name="hidSubmit" value="1" /&gt;
&lt;/fieldset&gt;
&lt;/form&gt;


&lt;?php
require_once 'footer_admin.php';
?&gt;
The big disadvantage of putting the validation to the setter is that you are never able to set invalid values. In most cases this is fine, but in some scenarios a valid user input differs from a more general valid field content. In addition the validation is also executed to internal setter uses where know that they are valid.

That said, I recommend setters to be only setters and not more.

The validation itself can be placed in a separate method or even in a separate class (depending on the complexity).

If you only want to display one error message, throwing an exception within the validation should be fine.

If you want to collect multiple error messages, you might want to build a generic validation class:

$valid=new Validation();
$valid-&gt;assertNotEmpty($name,"name","Insert a name please");
$valid-&gt;assertLength($name,3,50,"name","...");
...
if ($valid-&gt;hasErrors()) ...


You could extend this to a CourseValidation if you need it at multiple locations.

$valid=new CourseValidation($code,$name);
...