multiSelectField with pre-selected options in Yesod

扶醉桌前 提交于 2020-01-04 04:40:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!