问题
I have a many-to-many relationship between two entities. For example, let's think about a blogpost which can have multiple authors. This could be a simple model for it:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name String
age Int Maybe
deriving Show
Blogpost
title String
deriving Show
PersonBlogpost
person PersonId
blogpost BlogpostId
|]
My idea is to have an "Edit" page for a Blogpost entity, and in it have a multiSelectField for the authors, and have the attibute "selected" in the tags of the current authors. The user will be able to modify the authors and save his changes in the database. Whenever he enters again in the edit page of the same blogpost, the options he saved must appear selected in the multiSelectField.
I'm currently using an MForm like this:
(mAuthorsRes, mAuthorsView) <- mopt
(multiSelectField authors)
""
Nothing
authors
just have all the authors in the Person table. I know I have to replace the last Nothing
for the relevant code, but I don't know how to do it.
Thanks in advance.
来源:https://stackoverflow.com/questions/20588667/multiselectfield-with-pre-selected-options-in-yesod