PHP code example of ndaedzo / violin

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

    

ndaedzo / violin example snippets


use Violin\Violin;

$v = new Violin;

$v->validate([
    'name' => 'billy',
    'age' => 20
], [
    'name' => 'rrors()), '</pre>';
}

$v->addRuleMessage('isBanana', '%s expects banana, found "%s" instead.');

$v->addRule('isBanana', function($field, $value) {
    return $value === 'banana';
});

$v->addRuleMessage('

$v->addRuleMessages([
    ' to be an integer, but I found %s.',
]);

$v->addFieldMessage('username', '

$v->addFieldMessages([
    'username' => [
        ' your age.',
        'int' => 'Your age needs to be an integer.',
    ]
]);

class Validator extends Violin\Violin
{
    protected $db;

    protected function __construct(Database $db)
    {
        $this->db = $db; // Some database dependency

        // You can add a custom rule message here if you like, or, you
        // could add it outside of this validation class when you
        // make use of your new Validator object.
        $this->addRuleMessage('usernameDoesNotExist', 'That username is taken');
    }

    // Prepend your validation rule name with validate_
    public function validate_usernameDoesNotExist($field, $value)
    {
        if($db->where('username', '=', $value)->count()) {
            return false;
        }
    }
}

$v = new Validator;

// ... and so on.

$v->validate([
    'name' => 'billy',
    'age' => '20'
], [
    'name' => '

protected $errors = [
    'name' => [ // field
        'args' => [
            'FIELD_NAME', 'FIELD_VALUE'
        ],
        'errors' => [
            '