Smart Action checkbox defaultValue

In a smart action, I have a checkbox which I want to check false by default.
But this below doesn’t seem to work, it remains unchecked.

{
    name: 'Ajouter un contrat de maintenance',
    type: 'single',
    fields: [{
      ...
    }, {
      field: 'Tacite reconduction',
      type: 'Boolean',
      defaultValue: false
    }, {
      ...
    }]
  }

Hello @JeremyV,

Could you tell me which version of forest-express-sequelize or forest-express-mongoose, you use?
I’ve just tried with the latest, and add no issues.
You should see something like:
Capture d’écran 2021-06-23 à 19.18.17

Tell me if you get this.

Cheers,

"forest-express-sequelize": "^7.6.3",

Is it possible for you to test with the last version 7.11.3?

What do you get visually speaking?

I upgraded to 7.11.3 and I still have this:

image

Could you share with me which database you are using?
And how the the column is defined from an SQL point of view, please?

I use a remote postgreSQL DB.

Hello @JeremyV,

It is weird indeed.
Do you have defined any hooks on this smart action?
Could I get your whole smart action definition, please?

Thank you,

Here you are:

{
    name: 'Ajouter un contrat de maintenance',
    type: 'single',
    fields: [{
      field: 'Type de contrat',
      type: 'Enum',
      enums: [
        'Maintenance',
        'Assurance'
      ],
      isRequired: true
    }, {
      field: 'Catégorie de contrat',
      type: 'Enum',
      enums: [],
      isRequired: true
    }, {
      field: 'Clé de répartition',
      type: 'Enum',
      enums: [],
      isRequired: true
    }, {
      field: 'N° de contrat',
      type: 'String',
      isRequired: true
    }, {
      field: 'Date de début',
      type: 'Dateonly',
      isRequired: true
    }, {
      field: 'Date de fin',
      type: 'Dateonly',
      isRequired: true
    }, {
      field: 'Préavis en jours',
      type: 'Number',
      isRequired: true
    }, {
      field: 'Tacite reconduction',
      type: 'Boolean',
      defaultValue: false
    }, {
      field: 'Montant annuel TTC',
      type: 'Number',
      isRequired: true
    }, {
      field: 'Commentaire',
      type: 'String'
    }, {
      field: 'Périodicité du contrat d\'assurance',
      type: 'Enum',
      enums: [
        'Mensuelle',
        'Trimestrielle',
        'Annuelle'
      ]
    }, {
      field: 'Recherche fournisseur (3 lettres minimum)',
      type: 'String'
    }, {
      field: 'Fournisseur',
      type: 'Enum',
      enums: [],
      isRequired: true
    }, {
      field: 'PJ du contrat',
      type: 'File',
      isRequired: true,
      description: 'PDF uniquement'
    }],
    hooks: {
      load: ({ fields, record }) => models.repartitionKeys.findAll({
        where: { place_id: record.id }
      }).then(repartitionKeys => {
        fields['Clé de répartition'].enums = repartitionKeys
          .sort(
            (a, b) => {
              if (a.letter < b.letter) { return -1; }
              if (a.letter > b.letter) { return 1; }
              return 0;
            })
          .map(repartitionKey =>
            `${repartitionKey.id} - ${repartitionKey.letter}` +
            (repartitionKey.displayName ? ` - ${repartitionKey.displayName}` : '') +
            (repartitionKey.feesTotal ? ` - ${repartitionKey.feesTotal}` : ''));

        return models.maintenanceContractCategories.findAll()
          .then(result => {
            fields['Catégorie de contrat'].enums = result.sort(
              (a, b) => {
                if (a.displayName < b.displayName) { return -1; }
                if (a.displayName > b.displayName) { return 1; }
                return 0;
              }).map(category => {
                return `${category.displayName.toUpperCase()} - ${category.id}`
              });
            return fields;
          });
      }),
      change: {
        ['Recherche fournisseur (3 lettres minimum)']: ({ fields, record }) => {
          if (fields['Recherche fournisseur (3 lettres minimum)'].value.length > 2) {
            return axios.get(`${API_URL}/forest_admin/sage_providers`, {
              headers: {
                'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
                'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
              },
              params: {
                place_id: '',
                provider_name: fields['Recherche fournisseur (3 lettres minimum)'].value,
                siret: '',
                matricule: ''
              }
            }).then(res => {
              if (res.data && res.data.length > 0) {
                fields['Fournisseur'].enums = res.data.map(provider => `${provider.matricule} - ${provider.raison_sociale} - ${provider.site_ville}`)
              }
              return fields;
            });
          }
          return fields;
        }
      }
    }
  }

Does it work if you comment the hooks part?

It does work when I remove the hooks part ! Should I set the defaultValue in my hooks script ?

Hello @JeremyV,

I have been able to reproduce the issue with a load hook.
We will fix it and let you know when a new version will be available.

Thanks for your support! :smiley:

Hey @JeremyV :wave:

I would happy happy to know where you did find a reference to this defaultValue attribute (If you remember - but I’m guessing in the v6 documentation maybe?).

Support for defaultValue was dropped in v7 then re-introduced to fulfill the fact that the hook feature was not enabled for bulk/global smart action, but hooks should be the way to go for the type single. It was in fact introduced in v7 especially for this kind of application.

In the load hook, you should be able to directly set the value of the field, instead of relying on defaultValue.

Since the deprecation was not clear enough in the v7 release, we will still support it in the next release & we will add a deprecation warning on this. Since it is not documented anymore, I would still highly suggest to switch to hooks exclusively for single smart actions.

If anything is not working as expected with the hook feature (setting values, etc), feel free to ask, and I’ll take a look at the contribution Guillaume made to fix this issue in the meantime.

Thanks for your insight @jeffladiray

TBH I have no certainty about where I found the defaultValue field originally.
It might have been in the v6 documentation, or in the .forest-admin-schema.json file.

{
        "field": "Tacite reconduction",
        "type": "Boolean",
        "defaultValue": false,
        "enums": null,
        "isRequired": false,
        "reference": null,
        "description": null,
        "position": 7,
        "widget": null
      },

I took the habit to have a look at the file to check my available options.

2 Likes