Redirige les requêtes invalides vers la vue d'erreur
This commit is contained in:
17
src/Silex/Validation/CommentValidation.php
Normal file
17
src/Silex/Validation/CommentValidation.php
Normal 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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user