Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

phlexi-form

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phlexi-form

  • 0.6.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Phlexi::Form

Phlexi::Form is a flexible and powerful form builder for Ruby applications. It provides a more customizable and extensible way to create forms compared to traditional form helpers.

Ruby

Features

  • Customizable form components (input, select, checkbox, radio button, etc.)
  • Automatic field type and attribute inference based on model attributes
  • Built-in support for validations and error handling
  • Flexible form structure with support for nested attributes
  • Works with Phlex or erb views
  • Extract input from parameters that match your form definition. No need to strong paramters.
  • Rails compatible form inputs

Installation

Add this line to your application's Gemfile:

gem 'phlexi-form'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install phlexi-form

Usage

There are 2 ways to use Phlexi::Form:

Direct Usage

Phlexi::Form(User.new) do
  field(:name) do |name|
    render name.label_tag
    render name.input_tag
  end

  field(:email) do |email|
    render email.label_tag
    render email.input_tag
  end

  nest_one(:address) do |address|
    address.field(:street) do |street|
      render street.label_tag
      render street.input_tag
    end

    address.field(:city) do |city|
      render city.label_tag
      render city.input_tag
    end
  end

  render submit_button
end

Important

If you are rendering your form inline e.g.

render Phlexi::Form(User.new) {
  render field(:name).label_tag
  render field(:name).input_tag
}

Make sure you use {...} in defining your block instead of do...end This might be fixed in a future version.

Inherit form

class UserForm < Phlexi::Form::Base
  def form_template
    field(:name) do |name|
      render name.label_tag
      render name.input_tag
    end

    field(:email) do |email|
      render email.label_tag
      render email.input_tag
    end

    nest_one(:address) do |address|
      address.field(:street) do |street|
        render street.label_tag
        render street.input_tag
      end

      address.field(:city) do |city|
        render city.label_tag
        render city.input_tag
      end
    end

    render submit_button
  end
end


# In your view or controller
form = UserForm.new(User.new)

# Render the form
render form

# Extract params
form.extract_input({
  name: "Brad Pitt",
  email: "brad@pitt.com",
  address: {
    street: "Plumbago",
    city: "Accra",
  }
})

Advanced Usage

Custom Components

You can create custom form components by inheriting from Phlexi::Form::Components::Base:

class CustomInput < Phlexi::Form::Components::Base
  def template
    div(class: "custom-input") do
      input(**attributes)
      span(class: "custom-icon")
    end
  end
end

# Usage in your form
field(:custom_field) do |field|
  render CustomInput.new(field)
end

Theming

Phlexi::Form supports theming through a flexible theming system:

class ThemedForm < Phlexi::Form::Base
  class Builder < Builder
    private
    
    def default_theme
      {
        input: "border rounded px-2 py-1",
        label: "font-bold text-gray-700",
        # Add more theme options here
      }
    end
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/radioactive-labs/phlexi-form.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 02 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc