Skip to content

Validator class to validate form post values in a simple way.

License

Notifications You must be signed in to change notification settings

jorgemddev/PHP-Validator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Validator

Validator class to validate form post values in a simple way.

Usage


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 } ?>

Rules

  • 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

License

Released under the MIT license
Copyright (c) 2014 Ravi Kumar

About

Validator class to validate form post values in a simple way.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 64.7%
  • CSS 35.3%