Module: Playbook::PbFormsHelper

Included in:
PbKitHelper
Defined in:
lib/playbook/pb_forms_helper.rb

Instance Method Summary collapse

Instance Method Details

#pb_form_with(data: {}, validate: false, **kwargs, &block) ⇒ Object

Renders a pb form with ::Playbook::Forms::Builder, that can render Playbook kits in the most railsie way.

I.e.:

pb_form_with model: @user do |f|
  f.text_field :name
end

The form can also validate the fields, and trigger the validation automatically:

I.e.:

pb_form_with model: @user, validate: true do |f|
  f.text_field :name, required: true
end

Parameters:

  • data (Hash) (defaults to: {})

    hash of data attributes

  • validate (Boolean) (defaults to: false)

    whether validation should be triggered or not

See Also:

  • for other options


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/playbook/pb_forms_helper.rb', line 25

def pb_form_with(data: {}, validate: false, **kwargs, &block)
  data = data.merge("pb-form-validation" => validate)
  classname = ["pb-form", kwargs[:class]].join(" ")
  options = kwargs.merge(
    class: classname,
    data: data,
    builder: ::Playbook::Forms::Builder
  )

  content_for(:pb_js, javascript_tag(<<~JS))
    window.addEventListener("DOMContentLoaded", function() { PbFormValidation.start() })
  JS

  form_with(**options, &block)
end