# frozen_string_literal: true require "cgi" require "action_view/helpers/content_exfiltration_prevention_helper" require "action_view/helpers/url_helper" require "action_view/helpers/text_helper" require "active_support/core_ext/string/output_safety" require "active_support/core_ext/module/attribute_accessors" module ActionView module Helpers # :nodoc: # = Action View Form Tag \Helpers # # Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like # FormHelper does. Instead, you provide the names and values manually. # # NOTE: The HTML options disabled, readonly, and multiple can all be treated as booleans. So specifying # disabled: true will give disabled="disabled". module FormTagHelper extend ActiveSupport::Concern include UrlHelper include TextHelper include ContentExfiltrationPreventionHelper mattr_accessor :embed_authenticity_token_in_remote_forms self.embed_authenticity_token_in_remote_forms = nil mattr_accessor :default_enforce_utf8, default: true # Starts a form tag that points the action to a URL configured with url_for_options just like # ActionController::Base#url_for. The method for the form defaults to POST. # # ==== Options # * :multipart - If set to true, the enctype is set to "multipart/form-data". # * :method - The method to use when submitting the form, usually either "get" or "post". # If "patch", "put", "delete", or another verb is used, a hidden input with name _method # is added to simulate the verb over post. # * :authenticity_token - Authenticity token to use in the form. Use only if you need to # pass custom authenticity token string, or to not add authenticity_token field at all # (by passing false). Remote forms may omit the embedded authenticity token # by setting config.action_view.embed_authenticity_token_in_remote_forms = false. # This is helpful when you're fragment-caching the form. Remote forms get the # authenticity token from the meta tag, so embedding is unnecessary unless you # support browsers without JavaScript. # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the # submit behavior. By default this behavior is an ajax submit. # * :enforce_utf8 - If set to false, a hidden input with name utf8 is not output. # * Any other key creates standard HTML attributes for the tag. # # ==== Examples # form_tag('/posts') # # =>
# # <%= form_tag('/posts', remote: true) %> # # => ") end # see http://www.w3.org/TR/html4/types.html#type-name def sanitize_to_id(name) name.to_s.delete("]").tr("^-a-zA-Z0-9:.", "_") end def set_default_disable_with(value, tag_options) data = tag_options.fetch("data", {}) if tag_options["data-disable-with"] == false || data["disable_with"] == false data.delete("disable_with") elsif ActionView::Base.automatically_disable_submit_tag disable_with_text = tag_options["data-disable-with"] disable_with_text ||= data["disable_with"] disable_with_text ||= value.to_s.clone tag_options.deep_merge!("data" => { "disable_with" => disable_with_text }) end tag_options.delete("data-disable-with") end def convert_direct_upload_option_to_url(options) return options unless options.delete(:direct_upload) if respond_to?(:rails_direct_uploads_url) options["data-direct-upload-url"] = rails_direct_uploads_url elsif respond_to?(:main_app) && main_app.respond_to?(:rails_direct_uploads_url) options["data-direct-upload-url"] = main_app.rails_direct_uploads_url end options end end end end