コード例 #1
0
<?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());
}
コード例 #2
0
<?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());
}
コード例 #3
0
<?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());
}
コード例 #4
0
<?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());
}
コード例 #5
0
<?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());
}