简体   繁体   English

蒸气3-如何从Leaf模板形式填充模型的数组属性

[英]Vapor 3 - How to populate array property of model from Leaf template form

I have a Group , User and an App model. 我有一个GroupUser和一个App模型。 Within my Group model, I have a property var apps: [App] and I create a sibling relationship bw Group and User. 在我的组模型中,我有一个属性var apps: [App]并创建一个bw组和用户同级关系。

In my WebsiteController, I have 2 handlers: 在我的WebsiteController中,我有2个处理程序:

  1. createGroupHandler which handles the GET request at /groups/create createGroupHandler ,它处理/ groups / create中GET请求
  2. createGroupPostHandler which handles the POST request at /groups/create createGroupPostHandler ,它处理/ groups / create中POST请求

My problem is that after selecting app objects in my form in my createGroup.leaf template, when I create my new group no apps are being populated into my apps array. 我的问题是,在我的createGroup.leaf模板中的表单中选择了应用程序对象后,当我创建新组时,没有应用程序填充到我的应用程序数组中。

I created structs to represent the context being rendered in my createGroupHandler and to handle the data being passed in the post request for createGroupPostHandler 我创建了结构来表示在我的createGroupHandler中呈现的上下文并处理在createGroupPostHandler的发布请求中传递的数据

struct CreateGroupContext: Encodable {
    let title = "Create Group"
    let users: Future<[User]>
    let apps: Future<[App]>
}

struct CreateGroupData: Content {
    let name: String
    let apps: [App]
    let users: [String]?
}

The apps are properly loaded into the form but despite selecting them like in the image below, they aren't added to the array 这些应用程序已正确加载到表单中,但是尽管如下图所示选择了它们,但它们并未添加到数组中 在此处输入图片说明

My createGroupPostHandler looks like below. 我的createGroupPostHandler如下所示。 I don't know how to grab the selected apps and populate my apps: [App] array when I create my group is my issue , I feel like this is where I should be doing that, but I don't know how to grab that from the Leaf template. 我不知道如何获取选定的应用程序并填充我的apps: [App]创建群组时出现apps: [App]数组是我的问题 ,我觉得这是我应该做的事情,但是我不知道如何获取从Leaf模板中获取。

func createGroupPostHandler(_ req: Request, data: CreateGroupData) throws -> Future<Response> {
        let group = Group(name: data.name, apps: data.apps)
        return group.save(on: req).flatMap(to: Response.self) { group in
            guard let id = group.id else {
                throw Abort(.internalServerError)
            }

            var userSaves: [Future<Void>] = []
            for user in data.users ?? [] {
                try userSaves.append(User.addUser(user, to: group, on: req))
            }

            let redirect = req.redirect(to: "/groups/\(id)")
            return userSaves.flatten(on: req).transform(to: redirect)
        }
    }

This is what my createGroup.leaf looks like: 这是我的createGroup.leaf的样子: 在此处输入图片说明

I use index notation of naming input tags in HTML, such as score[0] , score[1] , etc. Vapor is then quite happy to parse this and create a dictionary (okay, technically not an array!). 我使用在HTML中命名input标签的索引符号,例如score[0]score[1]等。Vapor非常乐于解析它并创建字典(好的,从技术上讲,不是数组!)。

Use this notation in the Leaf file: 在Leaf文件中使用以下表示法:

#for(result in results) {
    <input type="text" name="score[#(index)]" value="#(result.score)">
}

The controller function: 控制器功能:

struct SaveResult:Content
{
    var score:[Int:String]
}

func saveRoll(_ request:Request) throws -> Future<View>
{
    return try request.content.decode(SaveResult.self).flatMap
    {
        result in
        for i in 0...(result.score.count) - 1
        {
            print( detail.score[i] )
        }
        return try self.index(request)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM