Dropdown(s) of constants/foreign keys on create/update from UI

Hey guys!

I am having a question, again… :slight_smile:

Expected behavior

I have the following model:

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
import { Schema, Document, model} from 'mongoose';

interface IItem extends Document {
  related_user_id: Schema.Types.ObjectId,
  note_type: number,
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = new Schema({
  'related_user_id': { type: Schema.Types.ObjectId, ref: 'user' },
  'item_type': Number,
}, {
  timestamps: false,
});

export default model<IItem>('items', schema, 'Items');

As of today, I created “smart fields” that actually translate the item_type (enum) constants to an understandable string as well as the foreign key/id which gets remplaced by the name of the user.

The issue is that I would like to go further on this and, when an “administrator” (forest admin user) tries to create/update an item, I would like this admin to have a dropdown list available which would contain all the foreign key available for the user_id field (all the ids from another collection, of course visually changed by the name related collection’s user). At the same time (but slightly different), instead of a text input, I would like the admin to have a dropdown with all the item_type enum values proposed

So, to summarize (on create/update):

  • UserID foreign key objectID (Text input) → Should become a dropdown list with all the available ids from the user’s collection (if possible, each id proposed should be visually the name of the related user, so it’s easier to understand)
  • NoteType enum (Text input or integer dropdown) → Should become a dropdown list with all the availble enum values transformed into strings (so it’s undrstandable)

Actual behavior

I can’t find a proper way to do it, so I copy/paste user ID from the other collection item

Thanks for your help :slight_smile:

Hello Maxime!

Before answering your question, I would like to be sure I understand your two requests correctly.

  1. You have a collection Items which is linked to a user via the related_user_id field, and you want to be able to select this linked user in a dropdown when creating/updating a record on it ?

  2. I’m not sure about that one, because you talk about note_type being the enum, and item_type being a enum, which in fact you both declare as Numbers. Could you explain this scenario more precisely please? :slightly_smiling_face:

Have a nice day!

Hello :slight_smile:

Yes that’s it. Currently, when i start to type the id, the choice is getting proposed as dropdown “emixam23” by example, but i still have to start to type the begining of the id

Sorry but I am working so much these days and as I “anonymize” my code, I just have misstyped… sorry! NoteType is a mistake of mine, it’s ItemType :slight_smile:

Thanks

Hi @Emixam23 :wave: what I understand, you need to define a different edit widget.
See the documentation to know oh to go to field settings. And select a dropdown widget to achieve your goal.
Let me know if it help.

Hey!

It worked, thanks :slight_smile:

Max,