# Providing defaults to array control element

**URL:** <https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867>\
**Category:** General\
**Created:** [June 30, 2022, 2:07pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867 "2022-06-30T14:07:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielBog](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@DanielBog](https://jsonforms.discourse.group/u/DanielBog)\
**Post date:** [June 30, 2022, 2:07pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867/1 "2022-06-30T14:07:04Z")

</div>

Hey,

I am pretty sure I am missing something, but I am wondering how you would use an array control and add elements with defaults once you hit an add button. I tried to use

```auto
this.addItem(composePaths(this.control.path, this.$options.data.length),
                createDefaultValue(this.control.schema))();

```

And it adds an element to the array, but does not add it to the data object, which in that case makes it hard for us to save it. I looked it up in the code and if I understand it correctly I should be able to call handleChange() which should then make it appear in the data object, but it only imports the DispatchPropsOfArrayControl which does not provide a handleChange method?  
 ![image](https://global.discourse-cdn.com/free1/uploads/jsonforms/original/1X/850c172a1ec3d641e77b53cf592e6ba31fd38cd4.png)

Is this even the correct approach?

Greetings and thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [July 3, 2022, 9:21pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867/2 "2022-07-03T21:21:31Z")

</div>

Hi @DanielBog,

the `addItem` method expects the path to the array. In the code snipped the `path` is amended with some `this.$options.data.length` index which points into the array. Can you try just handing over the `path` without the `composePaths`?

You can see this in action for example in the Vuetify renderer set [here](https://github.com/eclipsesource/jsonforms-vuetify-renderers/blob/124d9449d119093c93c5845c3be64a3d59d72ad0/vue2-vuetify/src/layouts/ArrayLayoutRenderer.vue#L342-L350).

---

<div class="post-metadata">

**Author:** ![DanielBog](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@DanielBog](https://jsonforms.discourse.group/u/DanielBog)\
**Post date:** [July 4, 2022, 3:12pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867/3 "2022-07-04T15:12:19Z")

</div>

Hey Stefan,

thanks for the answer. Leaving out the compose path of course makes sense but the bigger problem was my stupidity. We have custom renderers which expect default values defined in the schema and I would have expected the createDefaultValue method to make use of them. For this to work I just had to create a custom createDefaultValue method which uses these custom properties.

Nevertheless I am curious on how you would solve this issue and if it would not make sense to include a default property for control elements and enhance the createDefaultValues method to use them if they exist.

Greetings and thanks for the amazing framework,  
Daniel

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [July 4, 2022, 3:25pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867/4 "2022-07-04T15:25:58Z")

</div>

Hi @DanielBog,

Yes, `createDefaultValue` is very simplistic at the moment. Whenever we require `default` support we usually use AJV as it already offers the option to fill-in `default` values during validation. This then also works in combination with `createDefaultValue`. See [here](https://jsonforms.discourse.group/t/help-with-default-values/31/2) for more information.

---

<div class="post-metadata">

**Author:** ![DanielBog](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@DanielBog](https://jsonforms.discourse.group/u/DanielBog)\
**Post date:** [July 4, 2022, 3:47pm UTC](https://jsonforms.discourse.group/t/providing-defaults-to-array-control-element/867/5 "2022-07-04T15:47:47Z")

</div>

This was exactly what I was looking for. Thank you very much.
