Validation

Lemon has tool that simplifies validating. Let's start by validating request.

To validate request, there is method validate().

$validation = $request->validate([
    'name' => 'max:16'
]);

if (!$validation) {
    return template('foo', error: 'Bad data');
}

In this method we provide validating rules. Theese rules are in format request-key => rule. If some rule doesn't pass, this method returns false. If the key does not exist, it also returns false.

So if we send request with data name=foo, it will pass, however, if we send name=foobarbazfoobarbazfoobarbaz or foo=barr, it won't.

All the rules are separated by | and arguments for the rules are provided using :.

Let's see all the rules.

RuleDescription
numericValue is number
notNumericValue is not number (but it can contain numbers)
emailValue is email
urlValue is url
colorValue is hexadecimal color
max:numberValue is smaller than given number
min:numberValue is bigger than given number
regexValue matches regex
notRegexValue doesn't match regex
containsValue contains regex
doesntContainValue doesn't contain regex

Custom rules

To create custom rule, we first have to access the rules, to do that simply use \Lemon\Validator::rules() method. and then add the rule using rule() method.

\Lemon\Validator::rules()->rule('upperCase', function(string $target) {
    return ctype_upper($target);
});