Fixxed bugs Form Validation
This commit is contained in:
@@ -257,12 +257,18 @@ const formId = `f-${Math.random().toString(36).slice(2)}`;
|
||||
}
|
||||
|
||||
function validateField(name) {
|
||||
console.log(name);
|
||||
|
||||
const schema = getSchema();
|
||||
|
||||
console.log(schema);
|
||||
|
||||
if (!schema || !schema.shape || !schema.shape[name]) return;
|
||||
|
||||
const root = form.querySelector(
|
||||
`[data-field="${CSS.escape(name)}"]`,
|
||||
);
|
||||
console.log(root);
|
||||
if (!root) return;
|
||||
|
||||
const { ctrls, isRadioGroup, isMultiCheckbox, checkboxes } =
|
||||
@@ -309,7 +315,9 @@ const formId = `f-${Math.random().toString(36).slice(2)}`;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(value);
|
||||
const res = schema.shape[name].safeParse(value);
|
||||
console.log(res);
|
||||
setError(
|
||||
root,
|
||||
res.success
|
||||
@@ -323,12 +331,9 @@ const formId = `f-${Math.random().toString(36).slice(2)}`;
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
console.log(validateOn);
|
||||
|
||||
if (validateOn.includes("input")) {
|
||||
form.addEventListener("input", (e) => {
|
||||
const root = e.target?.closest?.("[data-field]");
|
||||
console.log("input:", root);
|
||||
if (!root) return;
|
||||
const name = root.getAttribute("data-field");
|
||||
if (name) validateField(name);
|
||||
@@ -339,7 +344,6 @@ const formId = `f-${Math.random().toString(36).slice(2)}`;
|
||||
"blur",
|
||||
(e) => {
|
||||
const root = e.target?.closest?.("[data-field]");
|
||||
console.log("blur:", root);
|
||||
if (!root) return;
|
||||
const name = root.getAttribute("data-field");
|
||||
if (name) validateField(name);
|
||||
@@ -350,7 +354,6 @@ const formId = `f-${Math.random().toString(36).slice(2)}`;
|
||||
if (validateOn.includes("change")) {
|
||||
form.addEventListener("change", (e) => {
|
||||
const root = e.target?.closest?.("[data-field]");
|
||||
console.log("change:", root);
|
||||
if (!root) return;
|
||||
const name = root.getAttribute("data-field");
|
||||
if (name) validateField(name);
|
||||
|
||||
Reference in New Issue
Block a user