PHP code example of iulyanp / flex-validator

1. Go to this page and download the library: Download iulyanp/flex-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/ */

    

iulyanp / flex-validator example snippets


$ php composer.phar 

$array = [
    'name' => 'Iulian Popa',
    'contact' => [
        'phone' => '07579940094',
        'address' => 'str, Ion Creanga nr.14 A bl. N2'
    ]
];

$rules = [
    'name' => Validator::notBlank(),
    'contact.phone' => Validator::notBlank()->numeric()->length(0, 10),
    'contact.address' => Validator::notBlank()->alnum('.')->length(6, 30),
];

$validator->array($array, $rules);

array:1 [
  "contact" => array:2 [
    "address" => array:2 [
      "alnum" => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
      "length" => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
    ]
  ]
]

$validator->disableRulesName();

$validator->validate($array, $rules);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

//    array:1 [
//      "contact" => array:2 [
//        "phone" => array:1 [
//          0 => ""07579940094" must have a length lower than 10"
//        ]
//        "address" => array:2 [
//          0 => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
//          1 => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
//        ]
//      ]
//    ]

$validator->useDotErrorKeys();
$validator->validate($array, $rules);

if (!$validator->isValid()) {
    var_dump($validator->getErrors());
}

//    array:2 [
//        "contact.phone" => array:1 [
//        "length" => ""07579940094" must have a length lower than 10"
//      ]
//      "contact.address" => array:2 [
//        "alnum" => ""str, Ion Creanga nr.14 A bl. N2" must contain only letters (a-z), digits (0-9) and ".""
//        "length" => ""str, Ion Creanga nr.14 A bl. N2" must have a length between 6 and 30"
//      ]
//    ]

use Respect\Validation\Validator;

$array = [
    'username' => '',
    'password' => '',
];
$rules = [
    'username' => [
        'rules' => Validator::notBlank(),
        'group' => 'login'
    ],
    'password' => [
        'rules' => Validator::notBlank(),
    ]
];

$validator = new FlexValidator();
$validator->validate($array, $rules);

// will return
array:2 [
  "login" => array:1 [
    "username" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
  "password" => array:1 [
    "notBlank" => "null must not be blank"
  ]
]

use Respect\Validation\Validator;

$array = [
    'username' => '',
    'password' => '',
];
$rules = [
    'username' => [
        'rules' => Validator::notBlank(),
        'group' => 'login'
    ],
    'password' => [
        'rules' => Validator::notBlank(),
    ]
];

$validator = new FlexValidator();
$validator->validate($array, $rules, 'auth');

// will return
array:2 [
  "login" => array:1 [
    "username" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
  "auth" => array:1 [
    "password" => array:1 [
      "notBlank" => "null must not be blank"
    ]
  ]
]