-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to validate if the field has been changed using valiForm$
?
#239
Comments
I don't think this is currently possible. 😐 |
I see. Finally I have solved my issue like this. validate:$<ValidateForm<Some>>(async values => {
const {issues} = await safeParseAsync(
SomeSchema, values, {abortEarly:true});
const formErrors: Record<string, string> = {};
if (issues) {
for (const issue of issues) {
formErrors[getDotPath(issue)!] = issue.message;
}
}
const {body} = values;
if (body === loaded.value.body) {
formErrors.body = "Not changed";
}
return formErrors;
}), I think it is not rare case that the validation on relationship between the initial (current) values and input values. validate:valiForm$(SomeSchema, $(({body}, errors) => {
if (body === loaded.value.body) errors.body = "Not changed";
}), |
I need more context. What is |
I sent a PR. pls look at that. |
@fabian-hiller Currently the modular-form couldn't treat the validation that need to refer the initial value of the form, so I couldn't show it on the stackbliz. I needed to check the update of a field of the form. |
Giving a real example, suppose the edit form of the blog post. |
I don't quite understand the use case. Do you want to send the form only when it is "dirty" and contains changes? |
@fabian-hiller Partially Yes. The dirty state should exist not only for the entire form but for the certain field or fields. |
There is a dirty state for each field. If I have a minimal StackBlitz reproduction, I can check if what you want is already possible. At the moment I am not interested in extending the functionality of Modular Forms because Valibot has a higher priority and the moment and I may rewrite Modular Forms anyway. |
@fabian-hiller The problem is a bit complicated for making example on the StackBlitz. The part relating with the validation goes like this: const [form, {Form, Field}] = useForm({
loader:{value: {
id:undefined as number|undefined,
form_id:app.account?.form_id ?? undefined,
body:app.account?.form_body,
email:undefined as string|undefined,
email_ok:undefined as boolean|undefined,
privacy:false as boolean,
}},
validate:valiForm$(InquirySchema, $(async (
{body, email, privacy}:Partial<Inquiry>, errors:FormErrors<Inquiry>
) => {
if (body === app.account?.form_body) {
errors.body = "フォームに内容を記入してください";
}
if (email && !privacy) {
errors.privacy = "プライバシーポリシーに同意頂く必要があります。";
}
})),
}) |
Sorry for not getting back to this at the same pace I usually do, but at the moment I can't find the time to investigate this further. I will try to come back later. |
I am making Qwik app that uses modular-forms with valibot.
I have tried to make valibot schema that validates if the field of the form has been changed.
But the Qwik doesn't permit the use of local variables, the valibot schema should be located at the module scope.
So I think there is no way to provide the initial value to the schema, that is needed for that validation.
Is there a way to inject the initial value to the valibot schema?
The text was updated successfully, but these errors were encountered: