Redirige les requêtes invalides vers la vue d'erreur

This commit is contained in:
2022-12-11 12:34:49 +01:00
parent 82254c2f79
commit 5ca17e7e59
9 changed files with 68 additions and 51 deletions

View File

@@ -0,0 +1,17 @@
<?php
namespace Silex\Validation;
final class CommentValidation
{
public static function isValidComment(array $post, bool $requiresName, array &$errors): bool
{
if ($requiresName) {
UserValidation::isValidName($post, $errors, 'name');
}
if (empty($post['content'])) {
$errors[] = 'Empty message';
}
return empty($errors);
}
}

View File

@@ -6,24 +6,18 @@ namespace Silex\Validation;
final class UserValidation
{
public static function isValidLogin(array &$post, array &$errors): bool
public static function isValidLogin(array $post, array &$errors): bool
{
if(empty($post['login'])) {
$errors[] = 'Login error';
}
self::isValidName($post, $errors);
if(empty($post['password'])) {
$errors[] = 'Password error';
}
return empty($errors);
}
public static function isValidUser(array &$post, array &$errors): bool
public static function isValidUser(array $post, array &$errors): bool
{
if(empty($post['login'])) {
$errors[] = 'Login empty error';
}
self::isValidName($post, $errors);
if(empty($post['password'])) {
$errors[] = 'Password empty error';
}
@@ -38,4 +32,14 @@ final class UserValidation
return empty($errors);
}
public static function isValidName(array $post, array &$errors, string $key = 'login'): bool
{
if(empty($post[$key])) {
$errors[] = 'Empty login';
} else if(strlen($post[$key]) > 32) {
$errors[] = 'Login too long';
}
return empty($errors);
}
}