svelte-forms-lib icon indicating copy to clipboard operation
svelte-forms-lib copied to clipboard

Validate initial data

Open kbytesys opened this issue 4 years ago • 2 comments

Sometime we populate some partial data in the initialValue option, but we have to trigger the submit in order validate those fields.

It could be nice if the library will try to validate every value != null or empty.

Something like this:

    const {form, errors, isValid} = createForm({
        initialValues: {
            name: "myname", <-- VALIDATE THIS
            surname: "", <-- NO VALIDATE
            email: null, <-- NO VALIDATE
        },
        validateInitialValues: true,

kbytesys avatar May 08 '20 11:05 kbytesys

Ive been looking into writing a test for my PR #27 and noticed that the initial error state is set in the same way that the error reset is done.

errors: () => util.assignDeep(initialValues, NO_ERROR),

This will cause the same issue as I was trying to fix in #26 . Really this should use the validation schema to create the state. It would be pretty straight forward for simple yup schemas. But really not sure about the possible complexities when using some of yups more advanced features??

Was thinking something like a yupAssignDeep function

As @kbytesys said, it would be nice if it was possible to trigger a validation.

subpx avatar Jun 01 '20 23:06 subpx

Looks like this may be a matter of moving the validation section out of handleSubmit and exporting it, allowing users to trigger validations as and when they please:

https://github.com/tjinauyeung/svelte-forms-lib/blob/master/lib/create-form.js#L114-L140

EDIT: @kbytesys for your specific use-case there's now a validateField export you can use on specific fields: https://github.com/tjinauyeung/svelte-forms-lib/blob/master/lib/create-form.js#L59

EDIT: fixed URLs

larrybotha avatar Jul 06 '20 04:07 larrybotha