From 5a85950b858b59c56f2f4a214affd858fb4a2b41 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 12 Apr 2020 01:55:25 +0300 Subject: [PATCH] front side validations --- src/components/courses/ManageCoursePage.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/components/courses/ManageCoursePage.js b/src/components/courses/ManageCoursePage.js index 1b7e042..3141097 100644 --- a/src/components/courses/ManageCoursePage.js +++ b/src/components/courses/ManageCoursePage.js @@ -38,8 +38,22 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) { })); } + function formIsValid() { + const { title, authorId, category } = course; + const errors = {}; + + if (!title) errors.title = "Title is required."; + if (!authorId) errors.author = "Author is required"; + if (!category) errors.category = "Category is required"; + + setErrors(errors); + // Form is valid if the errors object still has no properties + return Object.keys(errors).length === 0; + } + function handleSave(event) { event.preventDefault(); + if (!formIsValid()) return; setSaving(true); actions .saveCourse(course)