PHP code example of martynbiz / php-validator

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

    

martynbiz / php-validator example snippets


$validator = new MartynBiz\Validator();
$validator->setData($_POST);

$validator->check('email')
  ->isNotEmpty('Email address is blank')
  ->isEmail('Invalid email address');

$errors = $validator->getErrors();

$continue = $validator->isValid();

$validator->check('name');

$errors = $validator->check('email')
  ->isNotEmpty('Email address is blank')
  ->isEmail('Invalid email address')
  ->getErrors();

$validator->check('age', array('optional' => true))
  ->is...

// strings

$validator->check('name')
  ->isNotEmpty('Value must not be blank');

$validator->check('email')
  ->isEmail('Email address must be valid');

$validator->check('name')
  ->isLetters('Value must be letters');

$validator->check('name')
  ->isMinimumLength('Password must be at least 8 characters', 8);

$validator->check('name')
  ->isMaximumLength('Password must be no more than 16 characters', 16);

// numeric strings

$validator->check('amount')
  ->isNumber('Value must be a number');

$validator->check('profit')
  ->isPositiveNumber('Value must be a positive number');

$validator->check('loss')
  ->isNotPositiveNumber('Value must not be a positive number, negatives and zeros OK');

$validator->check('loss')
  ->isNegativeNumber('Value must be a negative number');

$validator->check('profit')
  ->isNotNegativeNumber('Value must not be a negative number, positives and zeros OK');

// date/time

$validator->check('publish_date')
  ->isDateTime('Value must be date/time format yyyy-mm-dd hh:mm:ss');

$validator->check('publish_date')
  ->isDate('Value must be date format yyyy-mm-dd');

$validator->check('meeting_time')
  ->isTime('Value must be time hh:mm:ss');

// passwords

$message = 'Password must contain upper and lower case characters, and have more than 8 characters';
$validator->check('password')
  ->isNotEmpty($message)
  ->hasLowerCase($message)
  ->hasUpperCase($message)
  ->hasNumber($message)
  ->isMinimumLength($message, 8);

// etc

$validator->check('username')
  ->isNotEmpty('Username missing')
  ->isMaximumLength('Username must not exceed 16 characters', 16);

$validator->logError('Could not load api');

class MyValidator extends Validator
{
    protected $userModel;

    // example constructor with dependency injection
    public function __construct($userModel, $data)
    {
        $this->userModel = $userModel;

        $this->setData($data);
    }

    // create new validation rule
    public function isUniqueEmail($message)
    {
        //check whether this email exists in the db
        //this is an example model, use your own models here
        $user = $this->userModel->findByEmail( $this->value );

        // log error - 
    public function isValid()
    {
        // first_name
        $this->check('first_name')
            ->isNotEmpty('First name missing');

        // last_name
        $this->check('last_name')
            ->isNotEmpty('Last name missing');

        $this->check('password')
            ->isValidPassword('Invalid password');

        return parent::isValid();
    }
}

$validator = new MyValidator($userModel, $_POST):

if ($validator->isValid()) {
    //..
}

composer