summaryrefslogtreecommitdiff
path: root/create_thread.php
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-07-20 17:25:03 -0600
committercflip <36554078+cflip@users.noreply.github.com>2021-07-20 17:25:03 -0600
commit04d30cfe16e11140c8efb22afd61f2386c35cd87 (patch)
tree56cd423d64d54e6f0993f7486aa264031d9a733a /create_thread.php
parent3c5828b1a787bffa6e886a4952741e4bcaeb43b9 (diff)
Handle input validation in abstract Form class
Diffstat (limited to 'create_thread.php')
-rwxr-xr-xcreate_thread.php33
1 files changed, 10 insertions, 23 deletions
diff --git a/create_thread.php b/create_thread.php
index d01eb26..11b278c 100755
--- a/create_thread.php
+++ b/create_thread.php
@@ -42,36 +42,23 @@ if (!Session::get()->is_signed_in()) {
<input type="submit" name="submit">
</form>
<?php
+include_once './includes/form/CreateThreadForm.php';
include_once './includes/model/Post.php';
include_once './includes/model/Thread.php';
include_once './includes/error.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING);
- $thread_subject = filter_input(INPUT_POST, 'thread_subject', FILTER_SANITIZE_STRING);
- $thread_cat = filter_input(INPUT_POST, 'thread_cat', FILTER_SANITIZE_NUMBER_INT);
+ $form = new CreateThreadForm();
+ $post_content = $form->validate_post_content($_POST['post_content']);
+ $thread_subject = $form->validate_thread_subject($_POST['thread_subject']);
+ $thread_category = $form->validate_thread_category($_POST['thread_cat']);
- $errors = array();
- if (empty($thread_subject) or !$thread_subject) {
- $errors[] = 'Thread subject cannot be empty';
- }
- if (empty($post_content) or !$post_content) {
- $errors[] = 'Thread body cannot be empty';
- }
+ $form->on_success(function () use ($post_content, $thread_subject, $thread_category) {
+ $thread_id = Thread::create($thread_subject, $thread_category);
+ Post::create($post_content, $thread_id, $thread_category);
- if (!empty($errors)) {
- $errstr = 'Please check the following problems: <ul>';
- foreach ($errors as $err) {
- $errstr .= '<li>' . $err . '</li>';
- }
- $errstr .= '</ul>';
- trigger_error($errstr);
- } else {
- $thread_id = Thread::create($thread_subject, $thread_cat);
- Post::create($post_content, $thread_id, $thread_cat);
-
- header("Location: viewthread.php?id=" . $thread_id);
- }
+ header("Location: /viewthread.php?id=" . $thread_id);
+ });
}
?>
</body>