Form value binding is overwritten by schema default values
See original GitHub issueDescribe the bug
The form v-model
binding is being overwritten by the value
in schema, so there’s no way to set a default set of values that supersedes schema.
My schema has several default values of its own to have areas of a form auto-filled e.g. checkboxes ticked and select dropdowns with a default entry. When users have filled this in and saved it, it outputs the new values which are saved to server. When a user returns to the page, I don’t have any way to use v-model
binding or :value
to supersede the default values from schema with the users saved values from the server
To Reproduce
- Create a form with a schema that has default values, and a “saved config” object with differing values
- Load the component and observe any value setting being overridden by schema.
Reproduction CodePen available here
Expected behavior
Any binding to the form by :values
or v-model
should be able to supersede the defaultValue
in the schema if the values don’t match. Essentially like Object.assign({}, myServerSavedValues, schemaFieldDefaultValue)
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Yes it works when checked isn’t used, my aim is basically to set all the default values in schema, then apply a saved “previous values config” if it exists. The problem is checkbox doesn’t ever set itself as true using
value: true
, you need to usechecked: true
as well like in #391. If I usechecked: true
, I can no longer override the checkbox values without click being used.The reason I use schema values is because the schema itself that I’m using has its own formatting and default values, I parse this into a VueFormulate schema with default values included, because on the user side they might have never opened the form so they won’t have any saved config, but it still needs to set some values to a default (Checked/unchecked/text value with a default/etc.).
Example: I want to set a form for a property to have “isHouse: true” as a default checkbox value, unless ofcourse the user has previously set the value to false in which case their config should override the default value. If a user has never opened the form they won’t have any config saved, so I need it to default to true. The reason I can’t simply build a key value object and apply it is because I’m already parsing the VueFormulate schema from a custom schema from a server, so I would need to pull out all the default values including nested groups (Which could be several layers deep) and build my own “default config” object. My hope was by using
value
in the schema, VueFormulate would handle this for me and I’ll be able to merge in my previously saved config over the schema default. In general that does work, except for checkboxes.I think overall if #391 gets a fix and I can use
value: true
to tick a checkbox, this will make checkboxes consistent and I won’t have any issue trying to do what I needHmm…this technique works fine for me on your example:
https://codepen.io/team/braid/pen/VwPYKKg?editors=1010
Change the lines on 75-77 if you change the values from
false
totrue
they check/uncheck — those can be considered your “default values”. If your schema declared thevalue
of an input to to befoobar
then you would need the model value of the checkbox to be ‘foobar’ (that’s what value does, change the checked value). And of course if you define it as “checked” in your schema, then that is a very high priority so it will check the box, personally I would leave those checked/value attributes out of your schema (which is what I did in your codepen).