connection form ok + start of validation
continuous-integration/drone/push Build is passing Details

pull/12/head
samuel 1 year ago
parent eb99f43a89
commit 00c3c43d1f

@ -35,13 +35,15 @@ $router = new AltoRouter();
$router->setBasePath($basePath); $router->setBasePath($basePath);
$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig);
$authController = new \App\Controller\AuthController(new \App\Model\AuthModel(), $twig); $authGateway = new \App\Gateway\AuthGateway($con);
$authController = new \App\Controller\AuthController(new \App\Model\AuthModel($authGateway), $twig);
$router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("GET", "/", fn() => $sampleFormController->displayForm());
$router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST));
$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig());
$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST));
$router->map("GET", "/register", fn() => $authController->displayRegister()); $router->map("GET", "/register", fn() => $authController->displayRegister());
$router->map("POST", "/register", fn() => $authController->confirmRegister($_POST));
$match = $router->match(); $match = $router->match();

@ -6,8 +6,7 @@ use App\Gateway\AuthGateway;
use App\Model\AuthModel; use App\Model\AuthModel;
use Twig\Environment; use Twig\Environment;
class AuthController class AuthController {
{
private AuthModel $model; private AuthModel $model;
private Environment $twig; private Environment $twig;
@ -15,14 +14,43 @@ class AuthController
* @param AuthModel $model * @param AuthModel $model
* @param Environment $twig * @param Environment $twig
*/ */
public function __construct(AuthModel $model, Environment $twig) public function __construct(AuthModel $model, Environment $twig) {
{
$this->model = $model; $this->model = $model;
$this->twig = $twig; $this->twig = $twig;
} }
public function displayRegister(){ public function displayRegister() {
echo $this->twig->render("display_register.html.twig",[]); echo $this->twig->render("display_register.html.twig", []);
}
public function confirmRegister(array $request) {
if (isset($request['username']) && isset($request['password']) && isset($request['confirmpassword']) && isset($request['email'])) {
$errors = $this->model->validationRegister($request["password"], $request['confirmpassword']);
if (empty($errors)) {
echo $this->twig->render("display_register_confirm.html.twig", [$request]);
} else {
$bad_fields = [];
foreach ($errors as $error_code) {
switch ($error_code) {
case AuthModel::PASSWORD_CONFIRM_NOT_EQUALS:
$bad_fields[] = "password";
$bad_fields[] = "confirmpassword";
break;
}
}
echo $this->twig->render("display_register.html.twig", ['bad_fields' => $bad_fields]);
}
return;
}
// Invalid request shape
http_response_code(400);
echo "la requêtte est invalide";
} }

@ -2,7 +2,25 @@
namespace App\Gateway; namespace App\Gateway;
use App\Connexion;
class AuthGateway class AuthGateway
{ {
private Connexion $con;
/**
* @param Connexion $con
*/
public function __construct(Connexion $con)
{
$this->con = $con;
}
public function insertAccount (string $username, string $password, string $email){
$this->con->exec("INSERT INTO AccountUser VALUES ($username,$password,$email)");
}
} }

@ -3,9 +3,30 @@
namespace App\Model; namespace App\Model;
use App\Controller\AuthController; use App\Controller\AuthController;
use App\Gateway\AuthGateway;
class AuthModel class AuthModel {
{
private AuthController $controller; public const PASSWORD_CONFIRM_NOT_EQUALS = 0;
private AuthGateway $gateway;
/**
* @param AuthGateway $gateway
*/
public function __construct(AuthGateway $gateway) {
$this->gateway = $gateway;
}
public function validationRegister(string $password, string $confirmPassword): array {
$errors = [];
if ($password != $confirmPassword) {
$errors[] = self::PASSWORD_CONFIRM_NOT_EQUALS;
}
// si pas d'erreurs alors on appelle la gateway
return $errors;
}
} }

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compte</title>
</head>
<body>
<center><h2>ERROR REGISTER</h2></center>
</body>
</html>

@ -53,11 +53,25 @@
input[type="submit"]:hover { input[type="submit"]:hover {
background-color: #0056b3; background-color: #0056b3;
} }
{% if 'password' in bad_fields %}
.form-group #password {
border-color: red;
}
{% endif %}
{% if 'confirmpassword' in bad_fields %}
.form-group #confirmpassword {
border-color: red;
}
{% endif %}
</style> </style>
<div class="container"> <div class="container">
<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">
<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>

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compte</title>
</head>
<body>
<center><h2>Nouveau Compte</h2></center>
</body>
</html>

@ -14,5 +14,6 @@
<p>description: {{ v.description }}</p> <p>description: {{ v.description }}</p>
{% endfor %} {% endfor %}
</body> </body>
</html> </html>
Loading…
Cancel
Save