Swiftpack.co - Package - nodes-vapor/submissions

Submissions 📩

Swift Version Vapor Version CircleCI codebeat badge codecov Readme Score GitHub license

Installation

Package.swift

Add Submissions to the Package dependencies:

dependencies: [
    ...,
    .package(url: "https://github.com/nodes-vapor/submissions.git", from: "2.0.0-beta")
]

as well as to your target (e.g. "App"):

targets: [
    ...
    .target(
        name: "App",
        dependencies: [... "Submissions" ...]
    ),
    ...
]

Introduction

Submissions was written to reduce the amount of boilerplate needed to write the common tasks of rendering forms and processing and validating data from POST/PUT/PATCH requests (PPP-request, or submission for short). Submissions makes it easy to present detailed validation errors for web users as well as API consumers.

Submissions is designed to be flexible. Its functionality is based around Fields which are abstractions that model the parts of a submission.

single values with its validators and meta data such as a label. Usually a form or API request involves multiple properties comprising a model. This can be modeled using multiple Fields.

Getting started 🚀

First make sure that you've imported Submissions everywhere it's needed:

import Submissions

Adding the Provider

"Submissions" comes with a light-weight provider that we'll need to register in the configure function in our configure.swift file:

try services.register(SubmissionsProvider())

This makes sure that fields and errors can be stored on the request using a FieldCache service.

Validating API requests

TODO

Validating HTML form requests

Submissions comes with leaf tags that can render fields into HTML. The leaf files needs to be copied from the folder Resources/Views/Submissions from Submissions to your project's Resources/Views. Then we can register Submissions' leaf tags where you register your other leaf tags, for instance:

var leafTagConfig = LeafTagConfig.default()
...
leafTagConfig.useSubmissionsLeafTags()
services.register(leafTagConfig)

You can customize where Submissions looks for the leaf tags by passing in a modified instance of TagTemplatePaths to useSubmissionsLeafTags(paths:).

In order to render a view that contains Submissions leaf tags we need to ensure that the Fields are added to the field cache and that the Request is passed into the render call:

let nameField = Field(key: "name", value: "", label: "Name")
try req.fieldCache().addFields([nameField])
try req.view().render("index", on: req)

In your leaf file you can then refer to this field using an appropriate tag and the key "name" as defined when creating the Field.

Tags

Input tags

The following input tags are available for your leaf files.

#submissions:checkbox( ... )
#submissions:email( ... )
#submissions:hidden( ... )
#submissions:password( ... )
#submissions:text( ... )
#submissions:textarea( ... )

They all accept the same number of parameters.

With these options:

Position | Type | Description | Example | Required? -|-|-|-|- 1 | key | Key to the related field in the field cache | "name" | yes 2 | placeholder | Placeholder text | "Enter name" | no 3 | help text | Help text | "This name will be visible to others" | no

File tag

To add a file upload to your form use this leaf tag.

#submissions:file( ... )

With these options:

Position | Type | Description | Example | Required? -|-|-|-|- 1 | key | Key to the related field in the field cache | "avatar" | yes 2 | help text | Help text | "This will replace your existing avatar" | no 3 | accept | Placeholder text | "image/*" | no 4 | multiple | Support multple file uploads | "true" (or any other non-nil value) | no

Select tag

A select tag can be added as follows.

#submissions:select( ... )

With these options:

Position | Type | Description | Example | Required? -|-|-|-|- 1 | key | Key to the related field in the field cache | "role" | yes 2 | options | The possible options in the drop down | roles | no 3 | placeholder | Placeholder text | "Select an role" | no 4 | help text | Help text | "The role defines the actions a user is allowed to perform" | no

The second option (e.g. roles) is a special parameter that defines the dropdown options. It has to be passed into the render call something like this.

enum Role: String, CaseIterable, Codable {
    case user, admin, superAdmin
}

extension Role: OptionRepresentable {
    var optionID: String? {
        return self.rawValue
    }

    var optionValue: String? {
        return self.rawValue.uppercased()
    }
}

let roles: [Role] = .
try req.view().render("index", ["roles": roles.allCases.makeOptions()] on: req)

🏆 Credits

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Siemen.

📄 License

This package is open-sourced software licensed under the MIT license.

Github

link
Stars: 13
Help us keep the lights on

Releases

2.0.0-rc.8 - Aug 14, 2019

Added

  • OptionRepresentable protocol (and an extension of Sequence of OptionRepresentable) to make it possible to use the #submissions:selecttag
try req.view().render("index", ["roles": roles.allCases.makeOptions()] on: req)
  • Documentation for dealing with html forms (including the select tag)

Changed

  • The file input tag leaf file now treats the multiple keyword as a boolean parameter

2.0.0-rc.7 - Jun 5, 2019

Added

  • A Leaf tag for the select HTML element. See https://github.com/nodes-vapor/submissions/pull/46 for an example of how to use it.

2.0.0-rc.6 - May 7, 2019

Changed

  • Consistently default to false for isRequired for all Field initializers

Fixed

  • Fixed Swift 5 compiler warnings related to redundant access modifiers
  • Make sure a Submission made from a Submittable is also used for making the Submittable's additional fields in Submittable.makeFields

2.0.0-rc.5 - Apr 5, 2019

Added

  • support for array values in URLEncoded forms

Changed

  • Field's properties are now public.

Fixed

  • removed redundant "form-control" class from file-input leaf.

All of these come, once again, courtesy of @MrMage!

2.0.0-rc.4 - Mar 28, 2019

Changed

  • SubmissionsDatas properties are now public as the type was already before. Thanks to @MrMage!