svelte-forms-lib
svelte-forms-lib copied to clipboard
Validate initial data
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,
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.
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