question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Form value binding is overwritten by schema default values

See original GitHub issue

Describe 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

  1. Create a form with a schema that has default values, and a “saved config” object with differing values
  2. 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:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mfcodeworkscommented, Mar 23, 2021

Hmm…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 to true they check/uncheck — those can be considered your “default values”. If your schema declared the value of an input to to be foobar 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).

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 use checked: true as well like in #391. If I use checked: 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 need

0reactions
justin-schroedercommented, Mar 23, 2021

Hmm…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 to true they check/uncheck — those can be considered your “default values”. If your schema declared the value of an input to to be foobar 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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

VueFormulate schema checkboxes don't match default value
VueFormulate schema doesn't set the default values for checkboxes, it always sets an empty string "" . Checkbox values only become boolean ...
Read more >
Set default values for fields or controls - Microsoft Support
This article explains how to set a default value for a table field or for a control on a form in an Access...
Read more >
How to pass the default value back as input value if you don't ...
The defaultValue can be defined in the initial state, and the defaultValue is overwritten when the value of input changes.
Read more >
Schema Incompatibilities | PostgreSQL - Prisma
Default values aren't represented in database. Problem. When adding the @default directive in a Prisma 1 datamodel, the default values for this field...
Read more >
General Rules for Default Values - Informatica Documentation
The default value must be either a NULL, a constant value, a constant expression, an ERROR function, or an ABORT function. For input/output...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found