#現状
新規登録の機能を作っているが、email登録の項目で、新しいアドレスを登録しようとすると、自分で作ったDuplicateの例外処理に引っかかってしまう。
その原因が特定できない。
signup.php
<?php // 新規登録 require_once(__DIR__ . '/../config/config.php'); $app = new MyApp\Controller\Signup(); $app->run(); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Sign Up</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div id="container"> <form action="" method="post" id="signup"> <p> <input type="text" name="email" placeholder="email" value="<?= isset($app->getValues()->email) ? h($app->getValues()->email) : ''; ?>"> </p> <p class="err"><?= h($app->getErrors('email')); ?></p> <p> <input type="password" name="password" placeholder="password"> </p> <p class="err"><?= h($app->getErrors('password')); ?></p> <div class="btn" onclick="document.getElementById('signup').submit();">Sign Up</div> <p class="fs12"><a href="/login.php">Log In</a></p> <input type="hidden" name="token" value="<?= h($_SESSION['token']); ?>"> </form> </div> </body> </html>
Signup.php
<?php namespace MyApp\Controller; class Signup extends \MyApp\Controller { public function run() { if ($this->isLoggedIn()) { header('Location: ' . SITE_URL); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $this->postProcess(); } } protected function postProcess() { // validate try { $this->_validate(); } catch (\MyApp\Exception\InvalidEmail $e) { // echo $e->getMessage(); // exit; $this->setErrors('email', $e->getMessage()); } catch (\MyApp\Exception\InvalidPassword $e) { // echo $e->getMessage(); // exit; $this->setErrors('password', $e->getMessage()); } // echo "success"; // exit; $this->setValues('email', $_POST['email']); if ($this->hasError()) { return; } else { // create user try{ $userModel = new \MyApp\Model\User(); $userModel->create([ 'email' => $_POST['email'], 'password' => $_POST['password'] ]); } catch (\MyApp\Exception\DuplicateEmail $e) { $this->setErrors('email', $e->getMessage()); return; } // redirect to login header('Location: ' . SITE_URL . '/login.php'); exit; } } private function _validate() { if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) { echo 'Invalid Token!'; exit; } if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { throw new \MyApp\Exception\InvalidEmail(); } if (!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) { throw new \MyApp\Exception\InvalidPassword(); } } } ?>
Model.php
<?php namespace MyApp; class Model { protected $db; public function __construct() { try { $this->db = new \PDO(DSN, DB_USERNAME, DB_PASSWORD); } catch (\PDOException $e) { echo $e->getMessage(); exit; } } } ?>
User.php
<?php namespace MyApp\Model; class User extends \MyApp\Model { public function create($values) { $stmt = $this->db->prepare("insert into users (email, password, created, modified) values (:email, :password, now(), now())"); $res = $stmt->execute([ ':email' => $values['email'], ':password' => password_hash($values['password'], PASSWORD_DEFAULT) ]); if ($res === false) { throw new \MyApp\Exception\DuplicateEmail(); } } } ?>
DuplicateEmail.php
<?php namespace MyApp\Exception; class DuplicateEmail extends \Exception { protected $message = 'Duplicate Email!'; } ?>
回答1件
あなたの回答
tips
プレビュー