PHP Form validation returning strange value
<p>I recently made a very simple MVC'ish framework for my own. Now im trying to expand that framework with some classes, so i can save some time in the future. The class i am trying to make now is a form validation class. I thought it was working quite fine, untill i made a function to check for the minimum length of a string. </p>

<p>The class :</p>

<pre><code>&lt;?php
/**
 * Form Validation Class
 */
class formValidation
{
    private $errorMessages = array();
    private $method;
    public $errorString;
    public $validationRules = array();

    public function setMethod($method)
    {
        $this-&gt;method = $method;
    }
    public function setRules($rules)
    {
        $this-&gt;validationRules = $rules;
    }
    public function run()
    {
        if (!empty($this-&gt;method)) {
            foreach ($this-&gt;method as $fieldname =&gt; $fieldvalue) {
                foreach ($this-&gt;validationRules as $rule) {
                    if ($rule['field'] == $fieldname) {
                        foreach ($rule as $option =&gt; $rulevalue) {
                            switch ($option) {
                                case 'required':
                                    if (!$this-&gt;checkRequired($fieldvalue)) {
                                        $this-&gt;errorMessages[] = $rule['name'] . " is a required field";
                                    }
                                    break;
                                case 'letters_numbers':
                                    if (!$this-&gt;checkLettersNumbers($fieldvalue)) {
                                        $this-&gt;errorMessages[] = $rule['name'] . " may only contain letters and numbers";
                                    }
                                case 'min_length':
                                    if (!$this-&gt;checkMinLength($fieldvalue, $rulevalue)) {
                                        $this-&gt;errorMessages[] = $rule['name'] . " must contain at least $rulevalue characters";
                                    }
                            }
                        }
                    }
                }
            }
            if (!empty($this-&gt;errorMessages[0])) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    // Checks
    private function checkRequired($value) // Makes a field mandatory
    {
        if ($value == "") {
            return false;
        } else {
            return true;
        }
    }
    private function checkLettersNumbers($value) // Allow Letters and Numbers only
    {
        return preg_match('/^[A-Za-z\s ]+$/', $value);
    }
    private function checkMinLength($value, $minlength) // Check input for minimum length
    {
        if (strlen($value) &lt; $minlength) {
            return false;
        } else {
            return true;
        }
    }
    // Error Handling Functions
    private function makeErrorString($prelimiter = '&lt;li&gt;', $delimiter = '&lt;/li&gt;') // Build all the errors into a string
    {
        if (!empty($this-&gt;errorMessages[0])) {
            $errors = "";
            foreach ($this-&gt;errorMessages as $message) {
                $errors .= $prelimiter;
                $errors .= $message;
                $errors .= $delimiter;
            }
            $this-&gt;errorString = $errors;
            return true;
        }
    }
    public function ValidationErrors() // Return the error string
    {
        if ($this-&gt;makeErrorString()) {
            return $this-&gt;errorString;
        }
    }
    /**
     * End of Class
     **/
}
/**
 * End of formValidation.php
 **/
?&gt;
</code></pre>

<p>And my controller file :</p>

<pre><code>&lt;?php
/**
 * Homepage Controller
 * @copyright 2012
 */
class home extends SimpleController
{
    public function index()
    {
        $view = new view('home');
        $val  = new formValidation();
        $val-&gt;setMethod($_POST);
        $rules = array(
                        array(
                            'field' =&gt; 'test',
                            'name' =&gt; 'Test',
                            'required' =&gt; true,
                            'letters_numbers' =&gt; true,
                            'min_length' =&gt; '5'
                        ),
                        array(
                            'field' =&gt; 'bla',
                            'name' =&gt; 'Trololol',
                            'required' =&gt; true
                        )
                     );

        $val-&gt;setRules($rules);
        if (!$val-&gt;run()) {
            echo $val-&gt;ValidationErrors();
        }
    }
}
?&gt;
</code></pre>

<p>In my view i have a form with 2 fields, test and bla. When i submit the form when it's empty, the checkMinLength() functions returns 2 value's :</p>

<p>Test must contain at least 1 characters
Test must contain at least 5 characters</p>

<p>First of all, where is it getting that 1 from, and 2nd why is it showing 2 messages for the same function ? I just can't find out why.</p>

<p>Hope you guys can help me out!
(The echo in the controller is just for demo)</p>

<p>Thanks!</p>
