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 📩
Installation
Package.swift
Add
Submissions
to the Package dependencies:as well as to your target (e.g. “App”):
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
Field
s 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
Field
s.Getting started 🚀
First make sure that you’ve imported Submissions everywhere it’s needed:
Adding the Provider
“Submissions” comes with a light-weight provider that we’ll need to register in the
configure
function in ourconfigure.swift
file: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
fromSubmissions
to your project’sResources/Views
. Then we can register Submissions’ leaf tags where you register your other leaf tags, for instance:You can customize where Submissions looks for the leaf tags by passing in a modified instance of
TagTemplatePaths
touseSubmissionsLeafTags(paths:)
.In order to render a view that contains Submissions leaf tags we need to ensure that the
Field
s are added to the field cache and that theRequest
is passed into therender
call: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.
They all accept the same number of parameters.
With these options:
File tag
To add a file upload to your form use this leaf tag.
With these options:
Select tag
A select tag can be added as follows.
With these options:
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.🏆 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.