Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use session for storing fieldset for next request #3

Open
Casperhr opened this issue Jan 4, 2017 · 0 comments
Open

Use session for storing fieldset for next request #3

Casperhr opened this issue Jan 4, 2017 · 0 comments

Comments

@Casperhr
Copy link
Contributor

Casperhr commented Jan 4, 2017

The pattern you are showing right now makes the browser remember the request as submit, and thereby refresh will submit again.

I created a flash system which (Will create a package just for that soon) (https://github.com/nodes-vapor/admin-panel/tree/master/Sources/AdminPanel/Support/Flash)

But I was doing the same pattern for this

  1. Middleware to add fieldset to session from response storage
    https://github.com/nodes-vapor/admin-panel/blob/master/Sources/AdminPanel/Support/Middlewares/FieldsetMiddleware.swift

  2. Extension to response for easier adding fieldset
    https://github.com/nodes-vapor/admin-panel/blob/master/Sources/AdminPanel/Support/Extensions/Response%2BFieldset.swift

  3. Pull out fieldset from Form, either from session or the static let
    https://github.com/nodes-vapor/admin-panel/blob/master/Sources/AdminPanel/Support/Extensions/Form%2BSession.swift

This will make it possible to make 2 funcs, 1-view 2-submit

View

public func create(request: Request) throws -> ResponseRepresentable {
        return try drop.view.make("BackendUsers/edit", [
            "fieldset": BackendUserForm.getFieldset(request)
        ], for: request)
    }

Submit

 public func store(request: Request) throws -> ResponseRepresentable {
        do {
            // Validate
            let backendUserForm = try BackendUserForm(validating: request.data)
            
            // Store
            var backendUser = BackendUser(form: backendUserForm)
            try backendUser.save()
     
            return Response(redirect: "/admin/backend_users").flash(.success, "User created")
        }catch FormError.validationFailed(let fieldSet) {
            return Response(redirect: "/admin/backend_users/create").flash(.error, "Validation error").withFieldset(fieldSet)
        }catch {
            return Response(redirect: "/admin/backend_users/create").flash(.error, "Failed to create user")
        }
    }

If you feel this is something you want in the package, I can make a PR next week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant