Module: Hanami::Helpers::FormHelper
- Includes:
- View::Helpers::TagHelper
- Included in:
- Extensions::View::StandardHelpers
- Defined in:
- lib/hanami/helpers/form_helper.rb,
lib/hanami/helpers/form_helper/values.rb,
lib/hanami/helpers/form_helper/form_builder.rb
Overview
Helper methods for generating HTML forms.
These helpers will be automatically available in your view templates, part classes and scope classes.
This module provides one primary method: #form_for, yielding an HTML form builder. This integrates with request params and template locals to populate the form with appropriate values.
Defined Under Namespace
Classes: FormBuilder, Values
Constant Summary collapse
- DEFAULT_METHOD =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default HTTP method for form
"POST"
- DEFAULT_CHARSET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default charset
"utf-8"
- CSRF_TOKEN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
CSRF Token session key
This name of this key is shared with the hanami and hanami-controller gems.
:_csrf_token
Instance Method Summary collapse
- #_form_csrf_token ⇒ Object private
- #_form_for_params ⇒ Object private
- #_form_for_values ⇒ Object private
-
#csrf_meta_tags ⇒ String?
Returns CSRF meta tags for use via unobtrusive JavaScript (UJS) libraries.
-
#form_for(base_name, url = nil, values: _form_for_values, params: _form_for_params, **attributes) ⇒ String
Yields a form builder for constructing an HTML form and returns the resulting form string.
Instance Method Details
#_form_csrf_token ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
206 207 208 209 210 |
# File 'lib/hanami/helpers/form_helper.rb', line 206 def _form_csrf_token return unless _context.request.session_enabled? _context.csrf_token end |
#_form_for_params ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
200 201 202 |
# File 'lib/hanami/helpers/form_helper.rb', line 200 def _form_for_params _context.request.params end |
#_form_for_values ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
188 189 190 191 192 193 194 195 196 |
# File 'lib/hanami/helpers/form_helper.rb', line 188 def _form_for_values if respond_to?(:_locals) # Scope _locals elsif respond_to?(:_name) # Part {_name => self} else {} end end |
#csrf_meta_tags ⇒ String?
Returns CSRF meta tags for use via unobtrusive JavaScript (UJS) libraries.
179 180 181 182 183 184 |
# File 'lib/hanami/helpers/form_helper.rb', line 179 def return unless (token = _form_csrf_token) tag.(name: "csrf-param", content: CSRF_TOKEN) + tag.(name: "csrf-token", content: token) end |
#form_for(base_name, url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String #form_for(url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String
Yields a form builder for constructing an HTML form and returns the resulting form string.
See FormBuilder for the methods for building the form’s fields.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/hanami/helpers/form_helper.rb', line 149 def form_for(base_name, url = nil, values: _form_for_values, params: _form_for_params, **attributes) url, base_name = base_name, nil if url.nil? values = Values.new(values: values, params: params, csrf_token: _form_csrf_token) builder = FormBuilder.new( base_name: base_name, values: values, inflector: _context.inflector, form_attributes: attributes ) content = (block_given? ? yield(builder) : "").html_safe builder.call(content, action: url, **attributes) end |