PHP code example of xphoenyx / valify

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

    

xphoenyx / valify example snippets


  $validator = new Validator();
 

use valify\Validator;


use valify\Validator;

class Model {
    /* ... */
    
    protected $validator;
    // Your rules
    public $rules = [];
    // Here is stored $_POST, for example
    public $data = [];

    function __construct() {
        /* ... */
        $this->validator = new Validator();
        /* ... */
    }

    /*
     * Your other methods
     */

    public function validate() {
        return $this->validator
            ->setRules($this->rules)
            ->loadData($this->data)
            ->validate();
    }

    public function getErrors() {
        return $this->validator->getErrors();
    }
}

$rules = [
    [['username', 'password'], 'string', 'max'=>10],
    ['email', 'email', 'message'=>'Please provide a valid email'],
    ['remember_me', 'boolean']
    /* ... */
];

['email', 'email', 'message'=>'{value} for attribute "{attribute}" is not a valid email'],

$rules = [
    /* ... */
    ['email', '\\examples\\ExampleValidator', 'ownProperty'=>'abc' /* ... */]
    /* ... */
];

$data = [
    'username'=>'username',
    'password'=>'123qwe',
    'email'=>'[email protected]',
    'remember_me'=>'1',
    /* ... */
];

$validator = new Validator();
$validator = $validator->setRules($rules)->loadData($data)

$validator = new Validator();
$validator = $validator
                ->setRules([...])
                ->loadData([...])
                ->setRules([...])
                ->setRules([...])
                ->loadData([...])
                ->loadData([...]);

$isValid = $validator->validate();

$password = $_POST['password'];
$isValid = Validator::validateFor('string', $password, ['min'=>6, 'max'=>20]);

$values = [
    $_POST['username'],
    $_POST['first_name'],
    $_POST['password'],
];
$isValid = Validator::validateFor('string', $values, ['min'=>3, 'max'=>30]);

if($validator->hasErrors()) {
    $errorMsgs = $validator->getErrors();
}

$errorMsgForUserAttr = $validator->getError('username');