I'm using the following validation function to validate forms on my site. I would like to extend it so that it supports arrays.
For example at the moment I might use it on the following form:
<input type="text" name="agency_name">
But I would like to adapt it so that it can validate fields in the following format (posted arrays):
<input type="text" name="agency_name[]">
<input type="text" name="agency_name[]">
The function I am using is:
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = $source[$item];
$item = escape($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($rule_value, array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} already exists.");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
}
Does anyone know how to modify it easily to support validation of array fields?
Thanks.