PHP code example of hasan-22 / form-validator

1. Go to this page and download the library: Download hasan-22/form-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hasan-22 / form-validator example snippets


composer 



$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
        ],
        'age'=>[
            'rules'=>['Required', 'Numeric'],
        ]
    ],$data);
    
print_r($result);

// Output: 
// [
//     [0] => [
//         [age] => The field `age` is 



$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
            'messages'=>['custom  
// [
//     [0] => [
//         [age] => custom 

    class Email implements \Validation\ValidationInterface{
    
        private array $formData;

        private array $errorMessage;
    
        private string $field;
    
        private string $additional;
    
        public function validate(){
            if(!filter_var($this->formData[$this->field], FILTER_VALIDATE_EMAIL)){
                return $this->errorMessage;
            }
        }
        
         public function formData(array $formData){
            $this->formData = $formData;
        }
        
        public function message(string $errorMessage){
            $this->errorMessage = [$this->field => empty($errorMessage) ? "The `{$this->field}` field is not an email" : $errorMessage];
        }
    
        public function field(string $field){
            $this->field = $field;
        }
        
        public function additionalData(string $additional)
        {
            $this->additional = $additional;
        }
    } 
    



$formData = ['email'=>'[email protected]'];
$result = \Validation\Validator::validate(
    [
        'email'=>[
            'rules'=>['Required','Email']
        ],
    ],$data);

//Output: 
//[] 
//If all validation passes, you get an empty array

$data = ['password'=>'123456','age'=>''];
$result = \Validation\Validator::validate(
    [
        'password'=>[
            'rules'=>['Regex:/[1-6]/']
        ]
    ],$data);