0

I have the following php form Validation class, which allows me to specify which form inputs are required, the length, and whether the input should be unique.

<?php
class Validate {
private $_passed = false,
        $_errors = array(),
        $_db = null;

public function __construct() {
    $this->_db = DB::getInstance();
}

public function check($source, $items = array()) {
    foreach($items as $item => $rules) {
        foreach($rules as $rule => $rule_value) {

            $value = trim($source[$item]);

            if($rule === 'required' && empty($value)) {
                $this->addError("{$item} is required.");
            } else if (!empty($value)) {

                switch($rule) {
                    case 'min':
                        if(strlen($value) < $rule_value) {
                            $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                        }
                    break;
                    case 'max':
                        if(strlen($value) > $rule_value) {
                            $this->addError("{$item} must be a maximum of {$rule_value} characters.");
                        }
                    break;
                    case 'matches':
                        if($value != $source[$rule_value]) {
                            $this->addError("{$rule_value} must match {$item}.");
                        }
                    break;
                    case 'unique':
                        $check = $this->_db->get('users', array($item, '=', $value));
                        if($check->count()) {
                            $this->addError("{$item} is already taken.");
                        }
                    break;
                }

            }

        }
    }

    if(empty($this->_errors)) {
        $this->_passed = true;
    }

    return $this;
}

protected function addError($error) {
    $this->_errors[] = $error;
}

public function passed() {
    return $this->_passed;
}

public function errors() {
    return $this->_errors;
}
}

My question is, at the moment the class returns an array of errors which I'm able to foreach through:

foreach($validate->errors() as $error) {
    echo $error;
}

but I'm wondering what I can do to allow me to show the errors individually - so I could show the relevant error under the relevant field on the form - as opposed to all as a block at the top.

I hope I've been able to explain that ok!!!

9
  • always validate on the client side.. Commented Nov 6, 2013 at 13:02
  • @AnshumanDwibhashi - why do you say that? Commented Nov 6, 2013 at 13:03
  • 1
    because validating on the client side is fast, it reduces the load on your server and it doesn't cause the user to wade through the annoying reload. @DanTemple Commented Nov 6, 2013 at 13:06
  • Agree with @DanTemple, perhaps this is a double check (to avoid inyections)... else you should validate on client side Commented Nov 6, 2013 at 13:22
  • I've just always validated server side - mostly to be sure no dodgy data is submitted Commented Nov 6, 2013 at 13:35

1 Answer 1

1

You could try to add some kind of key to the errors array, something like this:

protected function addError($key, $error) {
    $this->_errors[$key] = $error;
}

And on every call to addError function (I think you can use $rule as key, but if not then you should add one different key for every field, for example id of the html elements or name...):

$this->addError($item, "{$item} is already taken."); 

It's just an idea... let me know if it's not clear

EDITED: Changed $rule as key for $item

Sign up to request clarification or add additional context in comments.

1 Comment

What would be the best way of displaying the individual errors on the form page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.