noahsalvi / svelte-use-form

The most compact reactive form controller (including Validation) that you'll ever see.
MIT License
252 stars 14 forks source link

Set inital values, reset form and initzialize again with values #20

Closed IamSAB closed 2 years ago

IamSAB commented 3 years ago

How can I set the initial values for the form? As I understand, I cannot simply call useForm(data) where data is a plain javascript object, but have to build an object like: {name1: {initial: "myvalue}, name2: { initial: "myvalue"}}. Or how to do that programatically`

Further I tried to figure out, how to give new data to the form after initialization. Resetting works (calling $form.reset()) but assign new data with useForm() throws an error: Uncaught Error: Function called outside component initialization. II can set a new value via $form.name.element[0].value = "value", but then reset() function will not work anymore. Using the $form.name.value setter does not reflect the value in my form.

Thank you for the clarification. Like the simplicity use-form :D.

noahsalvi commented 3 years ago

Hey SAB

You're right the value property shouldn't be a setter, since it is not implemented to work as one ^^

At the moment you can set the initial value as you did in your example or by setting the value of the html element directly. <input name="somethingFields" value="This will be the initial value" />

Regarding setting the value after the reset, you could do the following: $form.controlName.reset({value: "This will be the new value"}); It isn't the cleanest way but idk it could help you. You could also try to set the value by setting it directly to the html element. (haven't tested that and it isn't a solution which I would recommend)

In the near future I would like to have a functioning setter for the value of a control but sadly im kinda busy right now. LMK if you have further questions or suggestions :)

maxlerebourg commented 2 years ago

Hi noahsalvi, any update on the reset feature? I like the simplicity of this form library, keep it up :D

noahsalvi commented 2 years ago

Hi noahsalvi, any update on the reset feature? I like the simplicity of this form library, keep it up :D

Hey sorry for the delay! By reset feature do you mean the thing i wanted to implement in the near future? If you are then good news, it's implemented and available through the method change on the FormControl object. e.g. $form.name.change("hello")

...But since you used "reset" im not fully sure that this is what you meant.