class Marten::Handlers::Schema
Overview
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.
Included Modules
Direct Known Subclasses
Defined in:
marten/handlers/schema.crmarten/handlers/schema/callbacks.cr
Class Method Summary
-
.schema_context_name(name : String | Symbol)
Allows to configure the name to use to include the schema into the template context.
-
.schema_context_name : String
Returns the name to use to include the schema object into the template context (defaults to
#schema
). -
.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.
-
.success_route_name : String | Nil
Returns the route name that should be resolved to produce the URL to redirect to when processing a valid schema.
-
.success_url(success_url : String | Nil)
Allows to configure a raw URL to redirect to when processing a valid schema.
-
.success_url : String | Nil
Returns the configured raw URL to redirect when processing a valid schema.
Instance Method Summary
- #initial_data
-
#post
Handles a
POST
HTTP request and returns aMarten::HTTP::Response
object. -
#process_invalid_schema
Produces the response when the processed schema is invalid.
-
#process_valid_schema
Produces the response when the processed schema is valid.
-
#put
Handles a
PUT
HTTP request and returns aMarten::HTTP::Response
object. -
#schema
Returns the schema, initialized using the request data.
-
#schema_class
Returns the schema class that should be used by the handler.
-
#success_url
Returns the URL to redirect to after the schema has been validated and processed.
Macro Summary
-
schema(schema_klass)
Allows to configure the schema class that should be used to process request data.
Instance methods inherited from class Marten::Handlers::Template
get
get
Class methods inherited from class Marten::Handlers::Template
template_name : String | Nil
template_name
Instance methods inherited from module Marten::Handlers::Rendering
get_response(content)
get_response,
render_template(context : Hash | NamedTuple | Nil | Marten::Template::Context)
render_template,
render_to_response(context : Hash | NamedTuple | Nil | Marten::Template::Context = nil)
render_to_response,
template_name : String
template_name
Instance methods inherited from class Marten::Handlers::Base
context
context,
delete
delete,
dispatch : Marten::HTTP::Response
dispatch,
get
get,
head(status : ::HTTP::Status | Int32) : HTTP::Responsehead head, json(raw_json : String, status : ::HTTP::Status | Int32 = 200)
json(serializable, status : ::HTTP::Status | Int32 = 200) json, options options, params : Marten::Routing::MatchParameters params, patch patch, post post, put put, redirect(url : String, permanent = false) redirect, render(template_name : String, context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200) render, request : Marten::HTTP::Request request, respond(streamed_content : Iterator(String), content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200)
respond(content = "", content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200) respond, response : Marten::HTTP::Response? response, response! response!, reverse(*args, **options)
reverse(*args, **options, &) reverse, trace trace
Constructor methods inherited from class Marten::Handlers::Base
new(request : HTTP::Request, params : Routing::MatchParameters)new(request : HTTP::Request, **kwargs) new
Class methods inherited from class Marten::Handlers::Base
content_security_policy_block
content_security_policy_block,
http_method_nameshttp_method_names(*method_names : String | Symbol) http_method_names
Instance methods inherited from module Marten::Handlers::Session
session(*args, **options)session(*args, **options, &) session
Instance methods inherited from module Marten::Handlers::RequestForgeryProtection
get_csrf_token
get_csrf_token,
referer_trusted?
referer_trusted?
Instance methods inherited from module Marten::Handlers::Flash
flash(*args, **options)flash(*args, **options, &) flash
Instance methods inherited from module Marten::Handlers::Cookies
cookies(*args, **options)cookies(*args, **options, &) cookies
Class Method Detail
Allows to configure the name to use to include the schema into the template context.
Returns the name to use to include the schema object into the template context (defaults to #schema
).
Allows to set the route name that should be resolved to produce the URL to when processing a valid schema.
Returns the route name that should be resolved to produce the URL to redirect to when processing a valid schema.
Defaults to nil
.
Allows to configure a raw URL to redirect to when processing a valid schema.
Returns the configured raw URL to redirect when processing a valid schema.
Defaults to nil
.
Instance Method Detail
Handles a POST
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
Produces the response when the processed schema is invalid.
By default, this will render the configured template and return a corresponding HTTP response.
Produces the response when the processed schema is valid.
By default, this will return a 302 redirect targetting the configured success URL.
Handles a PUT
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
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.
Macro Detail
Allows to configure the schema class that should be used to process request data.