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

Render empty array item fields when minItems is specified #434

Closed
n1k0 opened this issue Dec 28, 2016 · 6 comments
Closed

Render empty array item fields when minItems is specified #434

n1k0 opened this issue Dec 28, 2016 · 6 comments

Comments

@n1k0
Copy link
Collaborator

n1k0 commented Dec 28, 2016

When minItems is specified for an array field, we should initialize the form with as many empty fields as the number specified.

Reggino added a commit to Reggino/react-jsonschema-form that referenced this issue Feb 22, 2017
glasserc pushed a commit that referenced this issue Feb 24, 2017
* #434 - Render empty array item fields when minItems is specified

* Added tests + playground feature for minItems

* Lint issue

* Split tests: 2 assertions = 2 tests

* Don't fall back to array
@glasserc
Copy link
Contributor

glasserc commented Feb 24, 2017

This is closed by #484.

n1k0 added a commit that referenced this issue Mar 21, 2017
New features

* Add support for rows attribute of textarea widget. (#450)
* #434 - Render empty array item fields when minItems is specified (#484)
* Add a "has-danger" class to the form error list (#502)
* Show description for boolean fields (#498)
* Fix #488: Add a custom Form ErrorList prop.

Bugfixes

* Fix impossibility to use stateful ArrayFieldTeplate comp. (#519)
* Centralized shouldComponentUpdate handling in SchemaField (#490)
@n1k0
Copy link
Collaborator Author

n1k0 commented Mar 21, 2017

Released in v0.44.0.

@dropdevcoding
Copy link

I recently upgraded from 0.43.0 to 0.45.0 and it seems that this change causes unexpected behaviour in my application:

I have defined an array with minItems: 1 but this array is NOT marked as required. This will cause validation errors when submitting again without changing anything, since the changed default behaviour produces invalid state.

Could you please add an additional check respecting the required property before applying the newly introduced default behaviour?

@n1k0
Copy link
Collaborator Author

n1k0 commented Mar 28, 2017

In my own understanding of the JSON schema spec, required and minItems are not interdependent. You can have this schema:

{
  type: "object",
  properties: {
    foo: {
      type: "array",
      items: {type: "number"},
      minItems: 1
    }
  }
}

And both {} and {foo: [42]} would be valid payloads against that schema, while {foo: []} would not.

Edit: Oh I see what you mean, {foo: []} currently passes validation. That's a bug indeed. Could you please create a separate issue for tracking this? Thanks.

Edit2: I was using minLength in my example, while minItems should be used. Validation works as expected, so I still don't understand what is the exact problem you're referring to.

@spacebaboon
Copy link
Contributor

spacebaboon commented Mar 30, 2017

I'm having a problem with this change too. We're using an array with "ui:widget": "checkboxes" to render a checkbox group in a form, and minItems=1 to validate that the user has selected at least one.

It can be seen easily in the playground, under Arrays. Add "minItems": 1 to the multipleChoicesList section, and then delete the entry for multipleChoicesList in the formData section. When a form is first rendered, we have no formData, so this is the initial state, not an empty array.

Now click one of the checkboxes in the live demo and see what happens to the formData. A null entry is added after the selected values. Make it "minItems": 2 and 2 null entries are added. This also means that the minimum items validation does not work.

I thought that I could set a default for the field, by adding default: [] in the schema, but this changes the nulls to empty arrays.

It seems like minItems is serving two different purposes here. And perhaps default too. Perhaps default inside items could define the item default, and default outside could be the default for the array itself?

So my only option, unless I'm missing something, is to programmatically initialise the formData to be an empty array where I find an array type in the schema.

@spacebaboon
Copy link
Contributor

spacebaboon commented Apr 3, 2017

I think I'll post my comment above as a new issue, it looks like a bug to me, and this issue has already been closed.

[Edit: This is now posted as #534.]

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

No branches or pull requests

4 participants