Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form that show data; override/misuse the default value -> which makes all fields that have data NOT REQUIRED #9

Open
acidjunk opened this issue May 4, 2023 · 1 comment

Comments

@acidjunk
Copy link
Collaborator

acidjunk commented May 4, 2023

Currently on the python side we misuse a fields default value: for injecting the data on Form that are used to edit stuff.
This has a side effect: all form fields that have data, have a default -> so are not required anymore.

Currently we work around this with extra validators, which is error prone and leads to code duplication in cases where you have a Create and Edit Form.

Solution direction:

  1. it would be nice to embed the form data in the JSON scheme
  2. check if it possible to use the "uniforms" as a nested dict in the form field property: to provide the data (see screen, which shows an extra "value" key.)
  3. if 2 is not possible: try to return the data inside the JSON scheme on another location: frontend will need changes to populate the fields

Schermafbeelding 2023-05-04 om 13 28 08

Questions:

  • what to do with edit form when no data can be found for a field; but a default does exist on the form definition?
@acidjunk acidjunk changed the title When prepopulating forms with data Form that show data; override/misuse the default value -> which makes all fields that have data NOT REQUIRED May 4, 2023
@acidjunk
Copy link
Collaborator Author

acidjunk commented Jun 14, 2023

I fiddled a bit with some forms. Best way forward seems to be to delegate form data loading to the JS part, instead of polluting our pydantic models with defaults that hold the value.

JS part (example)

<AutoForm
    schema={bridge}
    onSubmit={this.submit}
    showInlineError={true}
    validate="onSubmit"
    model={{"str_field": "Data from backend", "number_field": 3, ...userInput}}
>

This works for all fields, except for "str": when you edit a pre-poulated string field: uniforms submits: "" -> which pydantic considers a valid string. When a required field isn't populated at all with data: a submit doesn't have this field in the payload: and that triggers a required validation.

Ugly backend workaround for the textfield

Define the field like this:

str_field: str = Field(..., min_length=1)

Probably good to convert all "" to None in the backend: pydantic/pydantic#2687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant