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

[bootstrap-4] File uploads result in error ".file should match format "data-url"" #2095

Closed
3 tasks done
dstaley opened this issue Oct 10, 2020 · 15 comments · Fixed by #3472
Closed
3 tasks done

[bootstrap-4] File uploads result in error ".file should match format "data-url"" #2095

dstaley opened this issue Oct 10, 2020 · 15 comments · Fixed by #3472
Labels
bootstrap-4 bootstrap-4 related theme issue bug

Comments

@dstaley
Copy link

dstaley commented Oct 10, 2020

Prerequisites

Description

Using the bootstrap-4 theme, submitting a file triggers an error:

.file should match format "data-url"

Steps to Reproduce

  1. Playground link
  2. Click "Choose file" for the first file upload
  3. Select any file from your computer
  4. Click "Submit"

Expected behavior

No errors are displayed.

Actual behavior

The following error is displayed:

.file should match format "data-url"

Version

master

@NeonMando158
Copy link

Is this issue due to Bootstrap or Something to do with the react-jsonschema wrapper ?

@epicfaace
Copy link
Member

Yeah, this appears to be a problem with the bootstrap-4 theme. Seems like the form value is incorrectly being set to the file path, instead of the data url representation of the file:

image

@anikethsaha @Xtremilicious is this something you might be able to look into?

@epicfaace epicfaace added bug bootstrap-4 bootstrap-4 related theme issue labels Dec 4, 2020
@Hasenn
Copy link
Contributor

Hasenn commented Jan 19, 2021

Are there any workarounds?

@tjuravich
Copy link

@Hasenn I worked around it using this:

I first created a widget in my repo called BootstrapFileWidget and reference the FileWidget from core:

import React from "react";
import FileWidget from "@rjsf/core/dist/cjs/components/widgets/FileWidget";

const BootstrapFileWidget = (props: any) => {
  return (
    <>
      <FileWidget {...props} type="file" />
    </>
  );
};

export default BootstrapFileWidget;

Then i popped that into the widgets

const widgets = {
  file: BootstrapFileWidget,
};

Then, made sure my file was using that widget:

const uiSchema = { file: { "ui:widget": "file" } }

Not the cleanest workaround, but it got me up in running in a few minutes

@NeonMando158
Copy link

@tjuravich Nice solution but what happens when you need more than two or three file upload fields ?
File Widget will only accommodate 1.

@tjuravich
Copy link

Still not the most elegant, but something like this:

const uiSchema = { fileFieldOne: { "ui:widget": "file" }, fileFieldTwo: { "ui:widget": "file" }}

@CKGrafico
Copy link

Super small update to @tjuravich great solution :)

 <>
      <Form.Label className={props.rawErrors?.length > 0 ? 'text-danger' : ''}>
        {props.label || props.schema.title}
        {(props.label || props.schema.title) && props.required ? '*' : null}
      </Form.Label>
      <div className="form-control-file">
        <FileWidget {...props} type="file" />
      </div>
    </>

To have label like in bootstrap theme

@epicfaace
Copy link
Member

epicfaace commented Mar 10, 2021

@tjuravich / @CKGrafico would you like to make a PR that fixes the bootstrap 4 file widget? 🙏

@CKGrafico
Copy link

Let'ts wait to the original author @tjuravich if not, ping me.

@peterkelly
Copy link

Here's another workaround which monkey-patches the library, without requiring you to do anything special in uiSchema:

import { withTheme, utils } from "@rjsf/core";
import { Theme } from "@rjsf/bootstrap-4";

const registry = utils.getDefaultRegistry();
const defaultFileWidget = registry.widgets["FileWidget"];
(Theme as any).widgets["FileWidget"] = defaultFileWidget;
const Form = withTheme(Theme);

The root cause of the bug is an incomplete implementation of FileWidget in the theme, which simply wraps around the text input. It doesn't work because obtaining data urls for the uploaded files requires some extra javascript functions, which are present in the core FileWidget.

@epicfaace
Copy link
Member

@boskjoett
Copy link

This bug is almost 1 year old and still not fixed - why?

@epicfaace
Copy link
Member

lol bugs don't fix themselves, someone has to fix it

@boskjoett
Copy link

This thread has identified the root cause (thanks peterkelly), so I was hoping some of the maintainers would fix it. I am unfortunately not a React expert.

@epicfaace
Copy link
Member

Also see #2418 for more context

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Mar 5, 2023
Fixes rjsf-team#2095 by making `FileWidget` use the `BaseInputTemplate` for rendering the file input
- Updated `@rjsf/utils` to fix two small bugs found while fixing the issue:
  - `dataURItoBlob()` was updated to deal with parsing errors in `atob()` with malformed URL and to decode the name to a human-readable string
    - Added additional tests to verify the fixes
  - `replaceStringParameters()` was updated to split using a regex to deal with situations where the params themselves contain replaceable character identifiers
    - Added an additional test to verify the bug was fixed
- Updated `@rjsf/core` to switch `FileWidget` so that it uses the `BaseInputTemplate` to render the input, passing a new `onChangeOverride` function to deal with file events
  - Updated `BaseInputTemplate` to favor `onChangeOverride` rather than the default `onChange` handler it builds
- Updated the `BaseInputTemplate` in all of the themes to also favor `onChangeOverride` if specified over the default `onChange` handler
  - Updated the snapshots in all themes to reflect the improved `FileWidget` use of `BaseInputTemplate` in all themes
- Updated `@rjsf/bootstrap-4` to delete the `FileWidget` in favor of the improved `core` one
- Updated `@rjsf/material-ui` and `@rjsf/mui` to have the `BaseInputTemplate` support the automatic `shrink` of the label when the `type` of the input is `date`, `datetime-local` and `file`
  - Also removed the `DateWidget` and `DateTimeWidget` in both themes since they were only providing the `shrink` behavior which is now encapsulated in `BaseInputTemplate`
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Mar 5, 2023
Fixes rjsf-team#2095 by making `FileWidget` use the `BaseInputTemplate` for rendering the file input
- Updated `@rjsf/utils` to fix two small bugs found while fixing the issue:
  - `dataURItoBlob()` was updated to deal with parsing errors in `atob()` with malformed URL and to decode the name to a human-readable string
    - Added additional tests to verify the fixes
  - `replaceStringParameters()` was updated to split using a regex to deal with situations where the params themselves contain replaceable character identifiers
    - Added an additional test to verify the bug was fixed
- Updated `@rjsf/core` to switch `FileWidget` so that it uses the `BaseInputTemplate` to render the input, passing a new `onChangeOverride` function to deal with file events
  - Updated `BaseInputTemplate` to favor `onChangeOverride` rather than the default `onChange` handler it builds
- Updated the `BaseInputTemplate` in all of the themes to also favor `onChangeOverride` if specified over the default `onChange` handler
  - Updated the snapshots in all themes to reflect the improved `FileWidget` use of `BaseInputTemplate` in all themes
- Updated `@rjsf/bootstrap-4` to delete the `FileWidget` in favor of the improved `core` one
- Updated `@rjsf/material-ui` and `@rjsf/mui` to have the `BaseInputTemplate` support the automatic `shrink` of the label when the `type` of the input is `date`, `datetime-local` and `file`
  - Also removed the `DateWidget` and `DateTimeWidget` in both themes since they were only providing the `shrink` behavior which is now encapsulated in `BaseInputTemplate`
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Mar 5, 2023
Fixes rjsf-team#2095 by making `FileWidget` use the `BaseInputTemplate` for rendering the file input
- Updated `@rjsf/utils` to fix two small bugs found while fixing the issue:
  - `dataURItoBlob()` was updated to deal with parsing errors in `atob()` with malformed URL and to decode the name to a human-readable string
    - Added additional tests to verify the fixes
  - `replaceStringParameters()` was updated to split using a regex to deal with situations where the params themselves contain replaceable character identifiers
    - Added an additional test to verify the bug was fixed
- Updated `@rjsf/core` to switch `FileWidget` so that it uses the `BaseInputTemplate` to render the input, passing a new `onChangeOverride` function to deal with file events
  - Updated `BaseInputTemplate` to favor `onChangeOverride` rather than the default `onChange` handler it builds
- Updated the `BaseInputTemplate` in all of the themes to also favor `onChangeOverride` if specified over the default `onChange` handler
  - Updated the snapshots in all themes to reflect the improved `FileWidget` use of `BaseInputTemplate` in all themes
- Updated `@rjsf/bootstrap-4` to delete the `FileWidget` in favor of the improved `core` one
- Updated `@rjsf/material-ui` and `@rjsf/mui` to have the `BaseInputTemplate` support the automatic `shrink` of the label when the `type` of the input is `date`, `datetime-local` and `file`
  - Also removed the `DateWidget` and `DateTimeWidget` in both themes since they were only providing the `shrink` behavior which is now encapsulated in `BaseInputTemplate`
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Mar 6, 2023
Fixes rjsf-team#2095 by making `FileWidget` use the `BaseInputTemplate` for rendering the file input
- Updated `@rjsf/utils` to fix two small bugs found while fixing the issue:
  - `dataURItoBlob()` was updated to deal with parsing errors in `atob()` with malformed URL and to decode the name to a human-readable string
    - Added additional tests to verify the fixes
  - `replaceStringParameters()` was updated to split using a regex to deal with situations where the params themselves contain replaceable character identifiers
    - Added an additional test to verify the bug was fixed
- Updated `@rjsf/core` to switch `FileWidget` so that it uses the `BaseInputTemplate` to render the input, passing a new `onChangeOverride` function to deal with file events
  - Updated `BaseInputTemplate` to favor `onChangeOverride` rather than the default `onChange` handler it builds
- Updated the `BaseInputTemplate` in all of the themes to also favor `onChangeOverride` if specified over the default `onChange` handler
  - Updated the snapshots in all themes to reflect the improved `FileWidget` use of `BaseInputTemplate` in all themes
- Updated `@rjsf/bootstrap-4` to delete the `FileWidget` in favor of the improved `core` one
- Updated `@rjsf/material-ui` and `@rjsf/mui` to have the `BaseInputTemplate` support the automatic `shrink` of the label when the `type` of the input is `date`, `datetime-local` and `file`
  - Also removed the `DateWidget` and `DateTimeWidget` in both themes since they were only providing the `shrink` behavior which is now encapsulated in `BaseInputTemplate`
- Updated the `CHANGELOG.md` file accordingly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bootstrap-4 bootstrap-4 related theme issue bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants