Module: PicoPhone::Rails::FormHelper

Defined in:
lib/pico_phone/rails/form_helper.rb

Overview

Included into ActionView::Base by the railtie once ActionView loads. Adds pico_phone_field_tag, a +text_field_tag+-like helper that displays national format for a valid number while leaving the underlying <input> free to submit whatever string the user types.

Named distinctly from Rails' own +phone_field_tag+/+f.phone_field+ (a built-in alias for telephone_field, plain <input type="tel"> with no formatting) rather than overriding it -- redefining a core Rails helper would silently change behavior for every phone_field call in an app, not just ones backed by a PicoPhone-managed attribute.

Examples:

pico_phone_field_tag :phone, @contact.phone, region: "US"

Instance Method Summary collapse

Instance Method Details

#pico_phone_field_tag(name, value = nil, region: nil, **options) ⇒ String

Returns an HTML-safe <input type="tel"> tag.

Parameters:

  • name (String, Symbol)
  • value (String, PicoPhone::PhoneNumber, nil) (defaults to: nil)
  • region (String, nil) (defaults to: nil)

    ISO 3166-1 alpha-2 region for interpreting value when it's a raw string

Returns:

  • (String)

    an HTML-safe <input type="tel"> tag



43
44
45
46
# File 'lib/pico_phone/rails/form_helper.rb', line 43

def pico_phone_field_tag(name, value = nil, region: nil, **options)
  display_value = PicoPhone::Rails.phone_field_display_value(value, region)
  text_field_tag(name, display_value, options.merge(type: "tel"))
end