add error messages #20

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

@ -29,14 +29,8 @@ class AuthController {
* @param ValidationFail[] $fails
* @return HttpResponse
*/
private function displayBadFields(string $viewName, array $fails): HttpResponse {
$bad_fields = [];
foreach ($fails as $err) {
if ($err instanceof FieldValidationFail) {
$bad_fields[] = $err->getFieldName();
}
}
return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]);
private function displayBadFields(string $viewName, array $fails): HttpResponse{
return ViewHttpResponse::twig($viewName, ['fails' => $fails]);
}
/**
@ -49,7 +43,7 @@ class AuthController {
"username" => [Validators::name(), Validators::lenBetween(2, 32)],
"password" => [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)) {
return $this->displayBadFields("display_register.html.twig", $fails);
@ -75,7 +69,7 @@ class AuthController {
$fails = [];
$request = HttpRequest::from($request, $fails, [
"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)) {
return $this->displayBadFields("display_login.html.twig", $fails);

@ -27,11 +27,11 @@ class AuthModel {
$errors = [];
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)) {
$errors[] = new FieldValidationFail("email", "email already exist");
$errors[] = new FieldValidationFail("email", "L'email existe déjà");
}
if(empty($errors)) {
@ -60,13 +60,13 @@ class AuthModel {
$errors = [];
if (!$this->gateway->mailExist($email)) {
$errors[] = new FieldValidationFail("email", "email doesnt exists");
$errors[] = new FieldValidationFail("email", "Vous n'êtes pas enregistré.");
return $errors;
}
$hash = $this->gateway->getUserHash($email);
if (!password_verify($password, $hash)) {
$errors[] = new FieldValidationFail("password", "invalid password");
$errors[] = new FieldValidationFail("password", "Mot de passe invalide.");
}
return $errors;

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

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

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

Loading…
Cancel
Save