0.0
No commit activity in last 3 years
No release in over 3 years
A library reflex to provide realtime form validation using the validations you already have
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.2, < 7.0
 Project Readme

FormReflex

FormReflex is a light extension of optimism, leveraging stimulus_reflex.

FormReflex provides real time form validation on change, using the validations already provided on your model.

Usage

Please follow the setup instructions for optimism here

Usage for FormReflex is identical to the optimism usage, with 2 exceptions:

  1. You must specify data-reflex="change->FormReflex#handle_change" on your form control
data-reflex="change->FormReflex#handle_change"
  1. The model you want to validate must be initialized in session[:model]

For example:

def new
  @post = Post.new
  session[:model] = @post unless @stimulus_reflex
end

private
def set_post
  @post = Post.find(params[:id])
  session[:model] = @post unless @stimulus_reflex
end

All in all, pulling in Optimism's own example, it would look like this:

<%= form_with(model: session[:model]) do |form| %>
  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, data: { reflex: "change->FormReflex#handle_change" } %>
    <%= form.error_for :name %>
  </div>

  <div class="field">
    <%= form.label :body %>
    <%= form.text_area :body, data: { reflex: "change->FormReflex#handle_change" } %>
    <%= form.error_for :body %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

You can also add models to an association easily with the add method. There's a corresponding delete method for removing models from an association. Simply add data-assocation="<name of association>" data-reflex="click->FormReflex#add"

<button type="button" data-association="post_details" data-reflex="click->FormReflex#add">
    Add Post Detail
</button>

Installation

Add this line to your application's Gemfile:

gem 'form-reflex'

And then execute:

$ bundle

Or install it yourself as:

$ gem install form-reflex

Contributing

Contribution directions go here.

License

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