front side validations

master
Tudor Stanciu 2020-04-12 01:55:25 +03:00
parent 0dfaea5949
commit 5a85950b85
1 changed files with 14 additions and 0 deletions

View File

@ -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)