All Projects → nodes-vapor → submissions

nodes-vapor / submissions

Licence: MIT license
Provides a common structure to deal with data based API requests

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to submissions

VaporElasticsearch
A Vapor/Swift Elasticsearch client
Stars: ✭ 26 (+73.33%)
Mutual labels:  vapor, vapor-3
VaporCRUDRouter
A Rails-inspired extension to Vapor's routing system
Stars: ✭ 58 (+286.67%)
Mutual labels:  vapor, vapor-4
pagination
Simple Vapor 3 Pagination
Stars: ✭ 64 (+326.67%)
Mutual labels:  vapor, vapor-3
ferno
Vapor Firebase Realtime database provider
Stars: ✭ 52 (+246.67%)
Mutual labels:  vapor, vapor-3
gatekeeper
Rate limiting middleware for Vapor 👮
Stars: ✭ 54 (+260%)
Mutual labels:  vapor, vapor-3
flash
Flash messages between views ⚡️
Stars: ✭ 34 (+126.67%)
Mutual labels:  vapor, vapor-3
apns
Vapor APNS for iOS
Stars: ✭ 59 (+293.33%)
Mutual labels:  vapor, vapor-3
KarmaAPI
Swift API using the Vapor web framework and Turnstile for authentication
Stars: ✭ 20 (+33.33%)
Mutual labels:  vapor
JSONAPISerializer
JSONAPISerializer for Server Side Swift
Stars: ✭ 21 (+40%)
Mutual labels:  vapor
sqlite-kit
Non-blocking SQLite client library with SQL builder built on SwiftNIO
Stars: ✭ 51 (+240%)
Mutual labels:  vapor
VaporGCM
A simple Android GCM/FCM library for Swift/Vapor
Stars: ✭ 25 (+66.67%)
Mutual labels:  vapor
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+346.67%)
Mutual labels:  vapor
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (+700%)
Mutual labels:  vapor
sanitize
Powerful model extraction from Vapor JSON requests
Stars: ✭ 17 (+13.33%)
Mutual labels:  vapor
OrderSystem
An independent micro-service that takes orders in and processes payments.
Stars: ✭ 16 (+6.67%)
Mutual labels:  vapor
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+340%)
Mutual labels:  vapor
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (+20%)
Mutual labels:  vapor
sqlite-nio
Non-blocking wrapper for libsqlite3-dev using SwiftNIO
Stars: ✭ 33 (+120%)
Mutual labels:  vapor
swiftybeaver-provider
SwiftyBeaver Logging Provider for Vapor, the server-side Swift web framework https://swiftybeaver.com
Stars: ✭ 32 (+113.33%)
Mutual labels:  vapor
apns
Helpful extensions and abstractions for using APNSwift
Stars: ✭ 75 (+400%)
Mutual labels:  vapor

Submissions 📩

Swift Version Vapor Version tests codebeat Readme Score GitHub license

Installation

Package.swift

Add Submissions to the Package dependencies:

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

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

targets: [
    ...
    .target(
        name: "App",
        dependencies: [
            ...
            .product(name: "Submissions", package: "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.

📄 License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].