PHP Classes

File: validates.php

Recommend this page to a friend!
  Classes of wiki chua   Flex Validator   validates.php   Download  
File: validates.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Flex Validator
Validate request values based on extensible rules
Author: By
Last change: add test example
Date: 2 years ago
Size: 1,314 bytes
 

Contents

Class file image Download
<?php
require __dir__.'/vendor/autoload.php';

use
WikiChua\FlexValidator\Validator;

if(isset(
$_POST['submit'])){
   
// customize rule
   
Validator::extend('strong_password',function($fieldname, $fieldvalue, $attributes){
        if (
preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#", $fieldvalue)){
            return
true;
        } else {
            return
false;
        }

    },
"Your password not strong enough.");


   
$Rules = [
       
'email' => [
           
'required',
           
'email',
        ],
       
'email2' => [
           
'required',
           
'email',
           
'different' => 'email2',
        ],
       
'password' => [
           
'required',
           
'confirmed' => 'password_confirmation',
           
'strong_password',
        ],
       
'password_confirmation' => [
           
'required',
        ],
       
'myfile' => [
           
'required',
           
'mimes' => [
               
'pdf','png'
           
]
        ],
       
'checkme' => [
           
'accepted',
        ],

    ];

   
$Messages = [
       
'email2' => [
           
'different' => 'Ok man. :field must be different from ' . $_POST["email"],
        ],
    ];

   
$valid = Validator::make($_POST,$Rules,$Messages);

    if(
$valid->fail())
    {
        echo
'<div class="alert alert-danger"><h3>ERRORS</h3><ul>';
        foreach (
$valid->getErrors() as $key => $value) {
            if(
is_array($value))
            {
                foreach (
$value as $value2) {
                    echo
'<li>'.$value2.'</li>';
                }
            }else
            {
                echo
'<li>'.$value.'</li>';
            }
        }
        echo
'</ul></div>';
    }
}