This is a very simplified version. But the essence of the problem is that, during the form check, if the field is not filled in, I want to display
error text over the input. And to do this, I need to get the error text from the catch (error) function CreateTitle to the parent
function FormErrorAction to then pass this text to < p > </p >. And also I need to get id from CreateTitle.
` const FormErrorAction = () => {
error = " data obtained from the function CreateTitle "
async function CreateTitle(data: FormData) {
"use server"
try {
const { title} = Object.fromEntries(data);
if (typeof title !== "string" || title.includes(" ") || title.length < 0) {
throw new Error("Fill in the field title");
} text = "Registration was successful";
const formId = new ObjectId().toString();
const array = JSON.stringify({ title, id: formId, });
await Create(array);
return formId
} catch (error) {
return error;
}
revalidatePath(/errorServerAction);
}
return (
<div>
<form
action={CreateTitle}
>
{
error && <p >{error}</p>
}
<label htmlFor="todo"> Fill in the field</label>
<input
type="text"
id="todo"
placeholder="title"
required
name="title"
/>
<button type="submit" value="Add post">
to begin
</button>
</form>
</div>
);
};
export default FormErrorAction;`
I tried to put the CreateTitle form handler function into a separate server file,
and make the function itself a client component. In the FormErrorAction function, create the clientAction function,
which should receive data from CreateTitle, check and transfer the received data to the useState hook ()
to then display the error. But I faced a new problem. So that the browser does not reload the form
event.preventDefault () is needed, and the action attribute only works with data: FormData. I also tried to get data using collback,
but the result`
"use server"
export async function CreateTitle(data: FormData) {
try {
const { title} = Object.fromEntries(data);
if (typeof title !== "string" || title.includes(" ") || title.length < 0) {
throw new Error("Fill in the field title");
} text = "Registration was successful";
const formId = new ObjectId().toString();
const array = JSON.stringify({ title, id: formId, });
await Create(array);
return formId
} catch (error) {
return error;
}
revalidatePath(/errorServerAction);
}
"use client"
const FormErrorAction = () => {
const [error, setError] = useState("");
const errorRef = useRef<HTMLFormElement>(null);
error = " data obtained from the function CreateTitle "
async function clientAction(data: FormData) {
const result = await CreateTitle(data);
if (typeof result === "string") {
const id = result;
await RevalidatePath();
} else {
if (result?.error) {
setError(result.error);
} else {
errorRef.current?.reset();
await RevalidatePath();
setError("Registration succeeded ");
}
}
}
return (
<div>
<form
action={clientAction}
ref={errorRef}
>
{
error && <p >{error}</p>
}
<label htmlFor="todo"> Fill in the field</label>
<input
type="text"
id="todo"
placeholder="title"
required
name="title"
/>
<button type="submit" value="Add post">
to begin
</button>
</form>
</div>
);
};
export default FormErrorAction;
` is the same.