Validator class to validate form post values in a simple way.
require_once 'classes/Database.php';
require_once 'classes/ErrorHandler.php';
require_once 'classes/Validator.php';
$db = new Database;
$errorHandler = new ErrorHandler;
$errosHtml = '';
if(!empty($_POST)) {
$validator = new Validator($db, $errorHandler);
$validation = $validator->check($_POST, [
'username' => [
'required' => true,
'maxlength' => 20,
'minlength' => 3,
'alnum' => true,
'unique' => 'users'
],
'email' => [
'required' => true,
'maxlength' => 255,
'email' => true,
'unique' => 'users'
],
'password' => [
'required' => true,
'minlength' => 7
],
'password_again' => [
'matches' => 'password'
]
]);
if( $validation->fails() ) {
//echo '<pre>', print_r( $validation, 1 ), '</pre>';
if( $validation->errors()->hasErrors('username') ) {
$errosHtml = '<li>' . implode( '</li><li>' , $validation->errors()->all('username') ) . '</li>';
}
if( $validation->errors()->hasErrors('email') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('email') ) . '</li>';
}
if( $validation->errors()->hasErrors('password') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password') ) . '</li>';
}
if( $validation->errors()->hasErrors('password_again') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password_again') ) . '</li>';
}
}
}
<?php if( $errosHtml ) { ?>
<ul class="alert error">
<?php echo $errosHtml; ?>
</ul>
<?php } ?>
- required: Returns FALSE if the form element is empty.
- minlength: Returns FALSE if the form element is shorter then the parameter value. minlength=>6
- maxlength: Returns FALSE if the form element is longer then the parameter value. maxlength=>10
- email: Returns FALSE if the form element does not contain a valid email address.
- activeemail: Returns FALSE if the form element does not contain a valid and active email address.
- url: Returns FALSE if the form element does not contain a valid url address.
- activeurl: Returns FALSE if the form element does not contain a valid and active url address.
- ip: Returns FALSE if the supplied IP is not valid.
- alpha: Returns FALSE if the form element contains anything other than alphabetical characters.
- alphaupper: Returns FALSE if the form element contains anything other than upper alphabetical characters.
- alphalower: Returns FALSE if the form element contains anything other than lower alphabetical characters.
- alphadash: Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.
- alphanum: Returns FALSE if the form element contains anything other than alpha-numeric characters.
- hexadecimal: Returns FALSE if the form element contains anything other than hexadecimal characters.
- numeric: Returns FALSE if the form element contains anything other than numeric characters.
- matches: Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
- unique: Returns FALSE if the form element is not unique to the table and field name in the parameter. unique[field]
Based on Alex Garrett work http://bit.ly/1oO8Yxn
Released under the MIT license
Copyright (c) 2014 Ravi Kumar