ForestAdmin / forest-express

🧱 Dependency of Express Lianas for Forest Admin
GNU General Public License v3.0
72 stars 21 forks source link

Problem : prefill a form with dynamic default values #205

Closed XavierColombel closed 6 years ago

XavierColombel commented 6 years ago

In Actions, I'm trying to prefill a form with dynamic default values. So I used the example here and it's like value function has no effect on fields. No field is prefilled.

Liana.collection('customers', {
  actions: [{
    name: 'Send money',
    fields: [{
      field: 'amount',
      isRequired: true,
      description: 'Amount',
      type: 'Number'
    }]
  }],
  values: (context) => {
      return {
        amount: 123
      };
  }
});

I'm running on Express server.

arnaudbesnier commented 6 years ago

Hi @XavierColombel, thanks for the feedback.

This is a documentation issue in the Mongoose section. The following code:

Liana.collection('customers', {
  actions: [{
    name: 'Send money',
    fields: [{
      field: 'amount',
      isRequired: true,
      description: 'Amount',
      type: 'Number'
    }],
    values: (context) => {
      return {
        amount: 123
      };
    }
  }]
});

should work as expected. The logic to pre-fill values is per action form, that's why it is defined in an action object.

We'll do the documentation fix next week.

🌲🌲🌲

XavierColombel commented 6 years ago

Thanks @arnaudbesnier but it's not working. Maybe you should try by yourself before you fix the documentation. It might be a bug elsewhere.

arnaudbesnier commented 6 years ago

Maybe you should follow this documentation example: https://doc.forestadmin.com/developer-guide/lumber.html#prefill-a-form-with-default-values

This feature is available on Single action type only. You need to specify this in your action too I guess.

Do you use the latest liana version?

XavierColombel commented 6 years ago

I use the last version of Liana "forest-express-mongoose": "^2.14.5" and when I put type="single", in an action, it simply removes all custom actions from the list.

Liana.collection("customers", {
    actions: [
        {
            name: "Send money",
            type: "single",
            fields: [
                {
                    field: "amount",
                    isRequired: true,
                    description: "Amount",
                    type: "Number",
                },
            ],
            values: context => {
                console.log(context);
                return {
                    amount: 123,
                };
            },
        },
    ],
});
arnaudbesnier commented 6 years ago

@XavierColombel, this is what I just tried:

Liana.collection('invoice', {
  actions: [{
    name: 'Charge',
    type: 'single',
    fields: [{
      field: 'foo',
      type: 'String',
    }, {
      field: 'value',
      type: 'Number',
    }],
    values() {
      return {
        foo: 'bar',
        value: 42,
      };
    },
  }],
});

and it worked as expected:

screen shot 2018-10-20 at 18 11 35
XavierColombel commented 6 years ago

It should be another problem. It's definitely not working on my side. Here is a video of my files and how it works, if you wanna have a look : https://streamable.com/0sgsc

arnaudbesnier commented 6 years ago

Hi @XavierColombel,

"Single" actions are actions only available from a record detail view. So, it is normal that, in your screencast, when you set the action as "Single" it disappears from the collection list actions. You have to click on a record to be able to the see the action. (See https://doc.forestadmin.com/developer-guide/lumber.html#configuring-the-accessibility-of-a-smart-action for more details).

Second point, the form pre-fill feature is only available only on "Single" type actions.

Let me know if your issue persists.

XavierColombel commented 6 years ago

@arnaudbesnier thank you for this explanation, it's clear for me now. I hope you could add this feature to list view, soon. 👍

XavierColombel commented 6 years ago

@arnaudbesnier, it's not working with Enums, right?

robertvanhoesel commented 5 years ago

Any plans to support settings default values on a bulk action?

The documentation does not mention this only works on single actions, took me quite some time to figure out it doesn't work on bulk. @arnaudbesnier – maybe update the docs to mention this?

arnaudbesnier commented 5 years ago

Hi @robertvanhoesel,

Do the default values you want to set in the Smart Action form depend on the selected records? If those values are always the same, you should be able to use the defaultValue option on the declared Smart Action fields.

The documentation mentions that the values function can be implemented for "single" actions only: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions#prefill-a-form-with-default-values

Screenshot 2019-07-21 at 22 47 25
robertvanhoesel commented 5 years ago

Hi @arnaudbesnier,

It's the same default value for all records. I was not aware of the defaultValue option, that definitely does the trick. Thanks!