add error messages #20

Closed
samuel.berion wants to merge 1 commits from deconnection into salva

@ -30,13 +30,7 @@ class AuthController {
* @return HttpResponse * @return HttpResponse
*/ */
private function displayBadFields(string $viewName, array $fails): HttpResponse{ private function displayBadFields(string $viewName, array $fails): HttpResponse{
$bad_fields = []; return ViewHttpResponse::twig($viewName, ['fails' => $fails]);
foreach ($fails as $err) {
if ($err instanceof FieldValidationFail) {
$bad_fields[] = $err->getFieldName();
}
}
return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]);
} }
/** /**
@ -49,7 +43,7 @@ class AuthController {
"username" => [Validators::name(), Validators::lenBetween(2, 32)], "username" => [Validators::name(), Validators::lenBetween(2, 32)],
"password" => [Validators::lenBetween(6, 256)], "password" => [Validators::lenBetween(6, 256)],
"confirmpassword" => [Validators::lenBetween(6, 256)], "confirmpassword" => [Validators::lenBetween(6, 256)],
"email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/","invalide"),Validators::lenBetween(5, 256)],
]); ]);
if (!empty($fails)) { if (!empty($fails)) {
return $this->displayBadFields("display_register.html.twig", $fails); return $this->displayBadFields("display_register.html.twig", $fails);
@ -75,7 +69,7 @@ class AuthController {
$fails = []; $fails = [];
$request = HttpRequest::from($request, $fails, [ $request = HttpRequest::from($request, $fails, [
"password" => [Validators::lenBetween(6, 256)], "password" => [Validators::lenBetween(6, 256)],
"email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/","invalide"),Validators::lenBetween(5, 256)],
]); ]);
if (!empty($fails)) { if (!empty($fails)) {
return $this->displayBadFields("display_login.html.twig", $fails); return $this->displayBadFields("display_login.html.twig", $fails);

@ -27,11 +27,11 @@ class AuthModel {
$errors = []; $errors = [];
if ($password != $confirmPassword) { if ($password != $confirmPassword) {
$errors[] = new FieldValidationFail("confirmpassword", "password and password confirmation are not equals"); $errors[] = new FieldValidationFail("confirmpassword", "Le mot de passe et la confirmation ne sont pas les mêmes.");
} }
if ($this->gateway->mailExist($email)) { if ($this->gateway->mailExist($email)) {
$errors[] = new FieldValidationFail("email", "email already exist"); $errors[] = new FieldValidationFail("email", "L'email existe déjà");
} }
if(empty($errors)) { if(empty($errors)) {
@ -60,13 +60,13 @@ class AuthModel {
$errors = []; $errors = [];
if (!$this->gateway->mailExist($email)) { if (!$this->gateway->mailExist($email)) {
$errors[] = new FieldValidationFail("email", "email doesnt exists"); $errors[] = new FieldValidationFail("email", "Vous n'êtes pas enregistré.");
return $errors; return $errors;
} }
$hash = $this->gateway->getUserHash($email); $hash = $this->gateway->getUserHash($email);
if (!password_verify($password, $hash)) { if (!password_verify($password, $hash)) {
$errors[] = new FieldValidationFail("password", "invalid password"); $errors[] = new FieldValidationFail("password", "Mot de passe invalide.");
} }
return $errors; return $errors;

@ -41,10 +41,10 @@ class Validators {
function (string $fieldName, string $str) use ($min, $max) { function (string $fieldName, string $str) use ($min, $max) {
$len = strlen($str); $len = strlen($str);
if ($len >= $max) { if ($len >= $max) {
return [new FieldValidationFail($fieldName, "field is longer than $max chars.")]; return [new FieldValidationFail($fieldName, "trop long, maximum $max caractères.")];
} }
if ($len < $min) { if ($len < $min) {
return [new FieldValidationFail($fieldName, "field is shorted than $min chars.")]; return [new FieldValidationFail($fieldName, "trop court, minimum $min caractères.")];
} }
return []; return [];
} }

@ -53,26 +53,32 @@
background-color: #0056b3; background-color: #0056b3;
} }
{% for err in bad_fields %} .error-messages{
.form-group #{{ err }} { color : #ff331a;
font-style: italic;
}
{% for err in fails %}
.form-group #{{ err.getFieldName() }} {
border-color: red; border-color: red;
} }
{% endfor %} {% endfor %}
</style> </style>
<div class="container"> <div class="container">
<center><h2>Se connecter</h2></center> <center><h2>Se connecter</h2></center>
<form action="login" method="post"> <form action="login" method="post">
<div class="form-group"> <div class="form-group">
{% for name in fails %}
<label class="error-messages"> {{ name.getFieldName() }} : {{ name.getMessage()}} </label>
{% endfor %}
<label for="email">Email :</label> <label for="email">Email :</label>
<input type="text" id="email" name="email" required> <input type="text" id="email" name="email" required>
<label for= "password">Mot de passe :</label> <label for= "password">Mot de passe :</label>
<input type="password" id="password" name="password" required> <input type="password" id="password" name="password" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="submit" value="S'identifier"> <input type="submit" value="S'identifier">

@ -49,12 +49,17 @@
cursor: pointer; cursor: pointer;
} }
.error-messages{
color : #ff331a;
font-style: italic;
}
input[type="submit"]:hover { input[type="submit"]:hover {
background-color: #0056b3; background-color: #0056b3;
} }
{% for err in bad_fields %} {% for err in fails %}
.form-group #{{ err }} { .form-group #{{ err.getFieldName() }} {
border-color: red; border-color: red;
} }
{% endfor %} {% endfor %}
@ -67,6 +72,11 @@
<center><h2>S'enregistrer</h2></center> <center><h2>S'enregistrer</h2></center>
<form action="register" method="post"> <form action="register" method="post">
<div class="form-group"> <div class="form-group">
{% for name in fails %}
<label class = "error-messages"> {{ name.getFieldName() }} : {{ name.getMessage() }} </label>
{% endfor %}
<label for="username">Nom d'utilisateur :</label> <label for="username">Nom d'utilisateur :</label>
<input type="text" id="username" name="username" required> <input type="text" id="username" name="username" required>
<label for= "password">Mot de passe :</label> <label for= "password">Mot de passe :</label>

Loading…
Cancel
Save