PHP code example of harp-orm / validate

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

    

harp-orm / validate example snippets


use Harp\Validate\Assert;
use Harp\Validate\Asserts;

$asserts = new Asserts(array(
    new Assert\Present('title'),
    new Assert\LengthBetween('title', 20, 100),
    new Assert\Email('newsletter_email'),
));

$subject = new stdClass();
$subject->title = 'small title';
$subject->newsletter_email = 'invalid email';

// title should be between 10 and 20 letters, newsletter_email should be a valid email
echo $asserts->getErrors($subject);

$errors = $asserts->getErrors($subject);

foreach($errors => $error) {
    echo $error->getName();
    echo $error->getMessage();
}

$errors = $asserts->validate($subject);

echo $errors->getFirst();
echo $errors->getNext();

use Harp\Validate\ValidateTrait;
use Harp\Validate\Asserts;
use Harp\Validate\Assert\Present;

class Model
{
    use ValidateTrait;

    public $test;

    public function getValidationAsserts()
    {
        return new Asserts(array(
            new Present('test'),
        ));
    }
}

new Callback('state', function ($subject, $value) {
    return $value !== 'test';
})

new Email('email_address')
new Email('email_address', 'some custom message')

new EmailStrict('email_address')
new EmailStrict('email_address', 'some custom message')

new GreaterThan('price', 20)
new GreaterThan('price', 20, 'some custom message')

new InArray('state', array('big', 'small'))
new InArray('state', array('big', 'small'), 'some custom message')

new IP('last_login_ip')
new IP('last_login_ip', 'some custom message')

new IsInstanceOf('state', 'My\Example\Item')
new IsInstanceOf('state', 'My\Example\Item', 'some custom message')

new LengthLessThan('name', 10, 200)
new LengthLessThan('name', 10, 200, 'some custom message')

new LengthEquals('name', 20),
new LengthEquals('name', 20, 'some custom message')

new LengthGreaterThan('name', 20)
new LengthGreaterThan('name', 20, 'some custom message')

new LengthLessThan('name', 20)
new LengthLessThan('name', 20, 'some custom message')

new LessThan('price', 20)
new LessThan('price', 20, 'some custom message')

new Matches('password', 'password_confirmation')
new Matches('password', 'password_confirmation', 'some custom message')

new IsInteger('quantity')
new IsInteger('quantity', 'some custom message')

new IsFloat('frequency')
new IsFloat('frequency', 'some custom message')

new Present('title')
new Present('title', 'some custom message if needed')

new RegEx('card_number', '/\d{20}/')
new RegEx('card_number', '/\d{20}/', 'some custom message')

new URL('website')
new URL('website', 'some custom message')

new URLStrict('website')
new URLStrict('website', 'some custom message')

class TestConfig {
    use AssertsTrait;
}

$config = new TestConfig();

$config
    ->assertPresent('name')
    ->assertURL('homepage', 'must have a valid homepage');

// Return the Asserts object
$config->getAsserts();