class

Marten::Handlers::Schema

Inherits Marten::Handlers::Schema::Callbacks / Marten::Handlers::Template / Marten::Handlers::Rendering / Marten::Handlers::Base / Marten::Handlers::XFrameOptions / Marten::Handlers::ContentSecurityPolicy / Marten::Handlers::Session / Marten::Handlers::RequestForgeryProtection / Marten::Handlers::Flash / Marten::Handlers::Cookies / Marten::Handlers::ExceptionHandling / Marten::Handlers::Callbacks / Marten::Core::DebugModeLoggable / Reference / Object

Handler allowing to process a form through the use of a schema.

This handler can be used to process a form and validate its data through the use of a schema. It is expected that the handler will be accessed through a GET request first: when this happens the configured template is rendered and displayed, and the configured schema which is initialized can be accessed from the template context in order to render a form for example. When the form is submitted via a POST request, the configured schema is validated using the form data. If the data is valid, the handler returns an HTTP redirect to a configured success URL.

class MyFormHandler < Marten::Handlers::Schema
  schema MyFormSchema
  template_name "my_form.html"
  success_route_name "my_form_success"
end

It should be noted that the redirect response issued will be a 302 (found).

The schema used to perform the validation can be defined through the use of the #schema macro. Alternatively, the #schema_class method can also be overridden to dynamically define the schema class as part of the request handling.

The #template_name class method allows to define the name of the template to use to render the schema while the #success_route_name method can be used to specify the name of a route to redirect to once the schema has been validated. Alternatively, the #sucess_url class method can be used to provide a raw URL to redirect to. The same method can also be overridden at the instance level in order to rely on a custom logic to generate the sucess URL to redirect to.

Class methods

schema_context_name(name : String | Symbol)

Allows to configure the name to use to include the schema into the template context.

Source
schema_context_name

Returns the name to use to include the schema object into the template context (defaults to schema).

Source
success_route_name(success_route_name : String | Nil)

Allows to set the route name that should be resolved to produce the URL to when processing a valid schema.

Source
success_route_name

Returns the route name that should be resolved to produce the URL to redirect to when processing a valid schema.

Defaults to nil.

Source
success_url(success_url : String | Nil)

Allows to configure a raw URL to redirect to when processing a valid schema.

Source
success_url

Returns the configured raw URL to redirect when processing a valid schema.

Defaults to nil.

Source

Instance methods

initial_data

Returns the initial data to use to initialize the schema.

Source
post

Handles a POST HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
process_invalid_schema

Produces the response when the processed schema is invalid.

By default, this will render the configured template and return a corresponding HTTP response.

Source
process_valid_schema

Produces the response when the processed schema is valid.

By default, this will return a 302 redirect targetting the configured success URL.

Source
put

Handles a PUT HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
schema

Returns the schema, initialized using the request data.

Source
schema_class

Returns the schema class that should be used by the handler.

Source
success_url

Returns the URL to redirect to after the schema has been validated and processed.

By default, the URL will be determined from the configured #success_url and #success_route_name values. This method can be overridden on subclasses in order to define any arbitrary logics that might be necessary in order to determine the schema success URL.

Source

Macros

schema(schema_klass)

Allows to configure the schema class that should be used to process request data.

Source

Nested types