
Pro validaci postu je třeba v dané metodě kontroleru přidat parametr typu Moony\bootstrap\core\services\Request, tato třída obsahuje metodu validate() a jejím prvním parametrem je pole. Toto pole by mělo obsahovat key = post input name a value = pole všech kontrol, které u tohoto inputu provést.
Key může začínat znakem:
? Tento input nemusí být součástí POST (lze použít při validaci pole)
% Pokud je tento input prázdný, tak žádná z validací nebude prováděná (neplatí pro validaci pole)
Při validaci se automaticky validuje také CSRF, proto je třeba v HTML formu použít {csrf}, viz. příklad níže. Pokud se má CSRF validace přeskočit, je třeba v metodě validate() uvést druhý parametr true.
<form action="{route()}" method="post"> {csrf} <input type="text" name="email"> <input type="password" name="password"> <input type="password" name="passwordConfirm"> </form>$validatorResult = $request->validate([ 'email' => Validator::email('Email není správně'), 'password' => Validator::length(8, null, 'Heslo musí mít alespoň {min} znaků'), 'passwordAgain' => Validator::equalToInput('password', 'Zadaná hesla nejsou shodná') ]); if($validatorResult->success()) { // hard work }else{ echo 'Chyba: ' . $validatorResult->getFirstFailMessage(); }
// String characters length limitations
// - Fail Message Variables: {min} {max}
Validator::length(?int $min = null, ?int $max = null, string $failMessage);
// Needs to be checked checkbox
Validator::checkboxChecked(string $failMessage);
// Throw InputNotValidException() inside closure for error message
// Return true (rules ok) or false (validation failed) inside closure
Validator::custom(function(array|string|null $inputValue, Request $request){ /*Validate $inputValue ...*/ });
// Needs to be email
Validator::email(?string $failMessage = null);
// Needs to be exact value or in values if array is used
Validator::equalTo(string|array $equalTo, ?string $failMessage = null);
// Needs to be equal to other posted input
Validator::equalToInput(string $inputName, ?string $failMessage = null);
// Uploaded file size limitations
// - Fail Message Variables: {maxSize} {fileSize} - (both are humanized)
Validator::fileSize(int|float $maxSizeInBytes , ?string $failMessage = null);
// Uploaded file type restrictions, use file extensions eg. pdf,png,jpg
Validator::fileType(string|array $allowedFileType, ?string $failMessage = null);
// If the file is roperly uploaded and waiting in temp dir
Validator::fileUploaded(?string $failMessage = null);
// Empty string is forbidden
Validator::notEmpty(?string $failMessage = null);
// The field needs to contain only number characters
Validator::number(?string $failMessage = null);
// Number needs to be in range of numbers
// - Fail Message Variables: {min} {max}
Validator::numberRange(?int $min = null, ?int $max = null, ?string $failMessage = null);
// Custom regex validation for input
Validator::regex(string $regex, ?string $failMessage = null);
// If the input match the regex, it fails
Validator::regexNot(string $regex, ?string $failMessage = null);
// Only for array validations, if 2 or more values are same, it fails
Validator::distinct(?string $failMessage = null);
// Only for array validations, if 2 or more values are same and are not empty values, it fails
Validator::distinctSkipEmpty(?string $failMessage = null);V případě potřeby validovat pole se upraví key = post input name a použije se dot asterisk (.*) syntaxe.
<form action="{route()}" method="post"> {csrf} <input type="text" name="email[]"> <input type="text" name="email[]"> <input type="text" name="email[]"> </form>$validatorResult = $request->validate([ 'email.*' => Validator::email('Email není správně'), ]); if($validatorResult->success()) { // hard work }else{ echo 'Chyba: ' . $validatorResult->getFirstFailMessage(); }
Pokud se jedná o input, který jako klíč pole obsahuje důležitou hodnotu (např. ID nějakého záznamu) je validace podobná. Např. chceme-li upravit cenu u více produktů najednou a ID toho produktu je klíčem pole (v tomto případě 2456, 3247 a 8523):
<form action="{route()}" method="post"> {csrf} <input type="text" name="price[2456]"> <input type="text" name="price[3247]"> <input type="text" name="price[8523]"> </form>$validatorResult = $request->validate([ 'price.*:key' => Validator::number('ID Produktu není číslo'), 'price.*:value' => Validator::number('Zadaná cena není číslo') ]); if($validatorResult->success()) { // hard work }else{ echo 'Chyba: ' . $validatorResult->getFirstFailMessage(); }