Hello, I would like to know if it is possible to put a default value to a related field on a smart action form.
It would look like something like this :
Sadly, the “Categorie” and “Sous_categorie” inputs are empty by default.
Expected behavior
I would like the “Categorie” and “Sous_categorie” inputs to be filled by default with the existing values.
Actual behavior
These 2 inputs are empty
Failure Logs
No failure logs
Code
Here’s the forest file :
const { collection } = require('forest-express-sequelize');
const { demandeAjoutModele, categorie, sousCategorie } = require('../models');
const _ = require('lodash');
collection('demandeAjoutModele', {
actions:
[
{
name: 'Accepter',
type: 'single',
endpoint: '/forest/accepter',
fields:
[
{
field: 'marque',
description: 'La marque du modèle (exemple: Sony)',
type: 'String',
isRequired: true
},
{
field: 'modele',
description: 'Le nom complet du modèle (exemple: Alpha 7S II)',
type: 'String',
isRequired: true
},
{
field: 'categorie',
reference: 'categorie.id',
isRequired: true
},
{
field: 'sous_categorie',
reference: 'sousCategorie.id',
isRequired: true
},
],
hooks: {
load: async ({ fields, request }) => {
const marqueField = fields.find(field => field.field === 'marque');
const modeleField = fields.find(field => field.field === 'modele');
const categorieField = fields.find(field => field.field === 'categorie');
const sousCategorieField = fields.find(field => field.field === 'sous_categorie')
const id = request.body.data.attributes.ids[0];
const demande = await demandeAjoutModele.findByPk(id);
const categorieEntity = await categorie.findOne({where: { id: demande.dataValues.categorieId }})
const sousCategorieEntity = await sousCategorie.findOne({where: { id: demande.dataValues.sousCategorieId }})
console.log(categorieEntity);
console.log(sousCategorieEntity);
categorieField.value = categorieEntity;
sousCategorieField.value = sousCategorieEntity;
marqueField.value = demande.marque;
modeleField.value = demande.modele;
return fields;
},
},
},
],
fields: [],
segments: [],
});
Thanks for your help