This is the code from the form validation class.
<?php
class Validator {
protected $errorHandler;
protected $rules = ['required', 'minlength', 'maxlength', 'email', 'alnum'];
public $messages = [
'required' => 'The :field field is required',
'minlength' => 'The :field field must be a minimum of :satisifer length',
'maxlength' => 'The :field field must be a maximum of :satisifer length',
'email' => 'That is not a valid email address',
'alnum' => 'The :field field must contain only letters or numbers'
];
public function __construct(ErrorHandler $errorHandler){
$this->errorHandler = $errorHandler;
}
public function check($items, $rules){
foreach($items as $item => $value){
if(in_array($item, array_keys($rules))){
$this->validate([
'field' => $item,
'value' => $value,
'rules' => $rules[$item]
]);
}
}
return $this;
}
public function fails(){
return $this->errorHandler->hasErrors();
}
public function errors(){
return $this->errorHandler;
}
protected function validate($item){
$field = $item['field'];
foreach($item['rules'] as $rule => $satisifer){
if(in_array($rule, $this->rules)){
if(!call_user_func_array([$this, $rule], [$field, $item['value'], $satisifer])){
$this->errorHandler->addError(
str_replace([':field', ':satisifer'], [$field, $satisifer], $this->messages[$rule]),
$field
);
}
}
}
}
protected function required($field, $value, $satisifer){
return !empty(trim($value));
}
protected function minlength($field, $value, $satisifer){
return strlen($value)>=$satisifer;
}
protected function maxlength($field, $value, $satisifer){
return strlen($value)<=$satisifer;
}
protected function email($field, $value, $satisifer){
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
protected function alnum($field, $value, $satisifer){
//Add any character to be allowed $isValid = array('-', '_');
$isValid = array(' ');
return ctype_alnum(str_replace($isValid, '', $value));
}
}
This is the error handler code
class ErrorHandler {
protected $errors = [];
public function addError($error, $key = NULL){
if($key){
$this->errors[$key][] = $error;
}else{
$this->errors[] = $error;
}
}
public function all($key = NULL){
return isset($this->errors[$key]) ? $this->errors[$key] : $this->errors;
}
public function hasErrors(){
return count($this->all()) ? true : false;
}
public function first($key){
return isset($this->all()[$key][0]) ? $this->all()[$key][0] : false;
}
}
As previously stated I can output the errors via print_r
if($validation->fails()){
echo '<pre>', print_r($validation->errors()->all()), '</pre>';
}
Array
(
[friend] => Array
(
[0] => The friend field is required
[1] => The friend field must be a minimum of 3 length
[2] => The friend field must contain only letters or numbers
)
[to] => Array
(
[0] => The to field is required
[1] => That is not a valid email address
)
[you] => Array
(
[0] => The you field is required
[1] => The you field must be a minimum of 3 length
[2] => The you field must contain only letters or numbers
)
[sender] => Array
(
[0] => The sender field is required
[1] => That is not a valid email address
)
)
I would like to know how to display the errors as html.
Any help welcome!
$errorHandler = new ErrorHandler;
$validator = new Validator($errorHandler);
$validation = $validator->check($_POST, [
'friend' => [
'required' => true,
'maxlength' => 20,
'minlength' => 3,
'alnum' => true
],
'to' => [
'required' => true,
'maxlength' => 255,
'email' => true
],
'you' => [
'required' => true,
'maxlength' => 20,
'minlength' => 3,
'alnum' => true
],
'sender' => [
'required' => true,
'maxlength' => 255,
'email' => true
]
]);
if($validation->fails()){
echo '<pre>', print_r($validation->errors()->all()), '</pre>';
}