I'm trying to send an email when the object error is empty but is not working, even if it is null and doesn't contain any error.
I've checked the validation with an console log, but is giving up the errors that are missing, but I don't know where is the cach.
Here is the contact form function:
import React, { useState, useRef } from 'react'
import './ContactForm.scss'
import emailjs from "emailjs-com"
import '../../shared/../../components/pages/Form/shared/LoginRegister.scss'
import * as styles from '../Form/Globals.module.scss'
import ContactValidation from '../../../utils/ContactValidation'
export default function ContactForm() {
const [values, setValues] = useState({ name: "", email: "", phone: "", subject: "", description: "" });
const [errors, setErrors] = useState({});
const form = useRef();
const ContactHandler = (e) => {
e.preventDefault();
setErrors(ContactValidation(values));
if (errors === 0 || errors === {} || errors === null || errors === []) {
emailjs.sendForm('api_hidden', 'swiftquiz_template', form.current, 'api_hidden')
.then((result) => {
console.log(result.text);
alert("Sent Successfully ^_^");
}, (error) => {
console.log(error.text);
});
setValues({ name: "", email: "", phone: "", subject: "", description: "" });
}
else {
console.log("All Fields are required");
}
}
return (
<>
{i_removed_components_for_simpicity}
</>
)
}
Here is the validation:
export default function ContactValidation(values){
let errors = {};
if(!values.name.trim()){
errors.name = "Name is required."
}
if(!values.email){
errors.email = "Email is required."
}else if(!/\S+@\S+\.\S+/.test(values.email)){
errors.email = "Email is invalid.";
}
if(!values.subject.trim()){
errors.subject = "Subject is required."
}
if(!values.description.trim()){
errors.description = "Description is required."
}
return errors;
}