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

arrayMinItems never populate #3796

Closed
1 task done
alekseyBatuhtin opened this issue Jul 28, 2023 · 2 comments
Closed
1 task done

arrayMinItems never populate #3796

alekseyBatuhtin opened this issue Jul 28, 2023 · 2 comments
Labels
feature Is a feature request

Comments

@alekseyBatuhtin
Copy link
Contributor

Prerequisites

What theme are you using?

core

Is your feature request related to a problem? Please describe.

Hi! I've met the next situation.
Schema:

{
  "definitions": {
    "singleFileUploader": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "uid": {
            "type": "string"
          },
          "url": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  },
  "type": "object",
  "required": [
    "fileUploader",
    "objectOfFields"
  ],
  "properties": {
    "fileUploader": {
      "$ref": "#/definitions/singleFileUploader"
    },
    "objectOfFields": {
      "type": "object",
      "properties": {
        "firstName": {
          "title": "First Name",
          "type": "string",
          "default": "John"
        }
      }
    }
  }
}

uiSchema:

{
  "fileUploader": {
    "ui:widget": "CustomFileUploader"
  }
}

CustomFileUploader own widget which consumes as data, an array of File meta

What I Want to get:
The user should attach a minimum 1 file:

    "singleFileUploader": {
      "type": "array",
      "minItems": 1,
      ...

If not, the user should be notified after validation, and need to show up an error about the required file.
It is not possible with the current behavior of prefilling default values.

Let's consider different options of defaultFormStateBehavior.

  1. Legacy:
    Link to playground
<Form />

formData:

{
  "fileUploader": [
    {}
  ],
  "objectOfFields": {
    "firstName": "John"
  }
}
  1. emptyObjectFields - skipDefaults

Link to playground

<Form 
  experimental_defaultFormStateBehavior={{
    emptyObjectFields: "skipDefaults"
}}
/>

formData:

{}

Describe the solution you'd like

What I would like to get:

  • objects and default values should be populated
  • arrays don't

Example:

<Form 
  experimental_defaultFormStateBehavior={{
    arrayMinItems: {
      populate: "never"
   }
}}
/>
{
  "fileUploader": [], // <- validation will say "It's empty, add minimum 1 element"
  "objectOfFields": {
    "firstName": "John" // <- it's default value everything is Ok
  }
}

What do you think about this?

Describe alternatives you've considered

No response

@alekseyBatuhtin alekseyBatuhtin added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Jul 28, 2023
@MarekBodingerBA
Copy link
Contributor

Thank you alekseyBatuhtin, I was just about to write about the exactly same problem, also for a custom file uploader. We also need never behaviour for populate of experimental_defaultFormStateBehavior.

Because of this, we've implemented custom solution that relies on patch-package which is not optimal. You can take a look at it here.

@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Aug 4, 2023
heath-freenome pushed a commit that referenced this issue Aug 4, 2023
* feat: arrayMinItems.populate = never

* fix: review comments

* move feature to 5.12.0 version
@alekseyBatuhtin
Copy link
Contributor Author

alekseyBatuhtin commented Aug 4, 2023

@MarekBodingerBA it will appear in the 5.12.0 version.

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

No branches or pull requests

3 participants