<?php

/**
 * Violin example. Errors for a specific field.
 * 
 * Simply getting a list of errors for a specific field that
 * occured while trying to validate the data passed in
 * against the rules given.
 */
require '../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->validate(['username' => ['dalegarrett1234567890', 'required|alpha|min(3)|max(20)'], 'email' => ['dale.codecourse.com', 'required|email']]);
var_dump($v->errors()->get('username'));
// Array of all 'username' errors.
<?php

/**
 * Violin example. Checking and getting first error.
 * 
 * This checks a specific field has an error, and then outputs
 * the first error that occured for that field. This is most
 * likely what you'd use in a real life situation within
 * your HTML form.
 */
require '../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->validate(['username' => ['dalegarrett1234567890', 'required|alpha|min(3)|max(20)'], 'email' => ['dale.codecourse.com', 'required|email']]);
if ($v->errors()->has('email')) {
    // Check if any errors exist for 'email'.
    echo $v->errors()->first('email');
    // First 'email' error (string).
}
<?php

/**
 * Violin example. Basic validation, two arrays.
 * 
 * This shows basic validation using two arrays, one for data,
 * and one for the ruleset. This is useful if you'd like to
 * extract the rules to a seperate class or variable.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$data = ['username' => 'dale', 'email' => '*****@*****.**', 'password' => 'ilovecats', 'password_confirm' => 'ilovecats'];
$rules = ['username' => 'required|alpha|min(3)|max(20)', 'email' => 'required|email', 'password' => 'required', 'password_confirm' => 'required|matches(password)'];
$v->validate($data, $rules);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
示例#4
0
use Violin\Violin;
$pdo = Database::connect();
$auth = new Auth($pdo);
if (!$auth->isLoggedIn()) {
    header("Location: index.php");
    die;
}
if (!empty($_POST)) {
    // keep track validation errors
    $usernameError = null;
    $passwordError = null;
    $valid = true;
    // keep track post values
    $username = $_POST['username'];
    $password = $_POST['password'];
    $v = new Violin();
    $v->validate(['username' => [$username, 'required|alpha|min(3)|max(20)'], 'password' => [$password, 'required|alpha|min(3)']]);
    if (!$v->passes()) {
        $usernameError = $v->errors()->get('username')[0];
        $passwordError = $v->errors()->get('password')[0];
        $valid = false;
    }
    $options = ["cost" => 12];
    $hashedPassword = password_hash($password, PASSWORD_BCRYPT, $options);
    // insert data
    if ($valid) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO user (username, password) values(?, ?)";
        $query = $pdo->prepare($sql);
        $query->execute(array($username, $hashedPassword));
<?php

/**
 * Violin example. Custom field messages.
 * 
 * Defining an error message for a particular field, when a
 * particular rule fails.
 *
 * This is the same as addFieldMessage, but allows adding
 * of multiple field messages in one go.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addFieldMessages(['username' => ['required' => 'We need a username to sign you up.', 'alpha' => 'Your username can only contain letters.'], 'email' => ['email' => 'That email doesn\'t look valid.']]);
$v->validate(['username' => ['cats4life', 'required|alpha|min(3)|max(20)'], 'email' => ['dale.codecourse.com', 'required|email'], 'password' => ['ilovecats', 'required'], 'password_confirm' => ['ilovecats', 'required|matches(password)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Message placeholders.
 * 
 * Shows how you can output all arguments as a comma seperated list,
 * if you have a variable amount of arguments for a rule.
 */
require '../../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessage('isoneof', '{field} must be one of {$*}');
$v->addRule('isoneof', function ($value, $input, $args) {
    $value = trim($value);
    return in_array($value, $args);
});
$v->validate(['age' => ['sheep', 'required|isoneof(apples, pears)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Message placeholders.
 * 
 * Shows the placeholders you can use when defining messages. You can
 * output the name of the field, the value given by the User, and
 * the arguments that were passed into the rule.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessage('between', 'The {field} must be between {$0} and {$1}, you gave {value}');
$v->validate(['age' => ['82', 'required|between(18, 35)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Custom rule message.
 * 
 * Defining an error message for when a particular rule fails.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessage('required', 'Hold up, the {field} field is required!');
$v->validate(['username' => ['', 'required|alpha|min(3)|max(20)'], 'email' => ['*****@*****.**', 'required|email'], 'password' => ['ilovecats', 'required'], 'password_confirm' => ['ilovecats', 'required|matches(password)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Custom rule.
 * 
 * Creating a custom rule using the addRule method, passing in a
 * closure which should return false if the check has failed,
 * or true if the check has passed.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessage('startsWithNumber', 'The {field} must start with a number.');
$v->addRule('startsWithNumber', function ($value, $input, $args) {
    $value = trim($value);
    return is_numeric($value[0]);
});
$v->validate(['username' => ['dale', 'required|min(3)|max(20)|startsWithNumber']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Custom field message.
 * 
 * Defining an error message for a particular field, when a
 * particular rule fails.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addFieldMessage('username', 'required', 'We need a username to sign you up.');
$v->validate(['username' => ['', 'required|alpha|min(3)|max(20)'], 'email' => ['*****@*****.**', 'required|email'], 'password' => ['ilovecats', 'required'], 'password_confirm' => ['ilovecats', 'required|matches(password)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Custom rule.
 * 
 * Creating a custom rule using the addRule method, passing in a
 * closure which should return false if the check has failed,
 * or true if the check has passed.
 *
 * This example shows the use of arguments that can be used
 * to make rules that require arguments.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessage('startsWith', 'The {field} must start with "{$0}".');
$v->addRule('startsWith', function ($value, $input, $args) {
    $value = trim($value);
    return $value[0] === $args[0];
});
$v->validate(['username' => ['dale', 'required|min(3)|max(20)|startsWith(a)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
示例#12
0
<?php

/**
 * Violin example. Basic validation.
 * 
 * This shows basic validation, passing in an array to the
 * validate method and checking if the validation passes
 * with the passes method. Then dumps errors if failed.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->validate(['username' => ['dale', 'required|alpha|min(3)|max(20)'], 'email' => ['*****@*****.**', 'required|email'], 'password' => ['ilovecats', 'required'], 'password_confirm' => ['ilovecats', 'required|matches(password)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Field aliases.
 * 
 * Shows using field aliases to rename fields without having
 * to define custom rule or field messages, making it a
 * cleaner solution if this will suffice.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->validate(['username|Username' => ['', 'required'], 'password|Password' => ['', 'required']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}
<?php

/**
 * Violin example. Custom rule message.
 * 
 * Defining an error message for when a particular rule fails.
 *
 * This is the same as addRuleMessage, but allows adding
 * of multiple rule messages in one go.
 */
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin();
$v->addRuleMessages(['required' => 'Hold up, the {field} field is required!', 'email' => 'That doesn\'t look like a valid email address to me.']);
$v->validate(['username' => ['', 'required|alpha|min(3)|max(20)'], 'email' => ['dale.codecourse.com', 'required|email'], 'password' => ['ilovecats', 'required'], 'password_confirm' => ['ilovecats', 'required|matches(password)']]);
if ($v->passes()) {
    // Passed
} else {
    var_dump($v->errors()->all());
}