🐾 Multistep form concept for Rails projects. Allows to create complex forms for a few models simultaneously. Supports selectable per step validations without data persistence into db.
Currently uses session to store data before actual save. If your sessions are stored in cookies then it has a 4 KB limit.
To start using it just add this line to your application's Gemfile:
gem 'schoolgirl_uniform'
Then you need to generate scaffold for future multistep form:
$ rails generate schoolgirl_uniform:install CatgirlsSurvey
You can also use snake case, so
catgirls_survey
would be identical toCatgirlsSurvey
and will generate the same output during scaffolding.
To achieve working multistep form you need to configure FVC:
e.g. CatgirlsSurveyForm - app/forms/catgirls_survey_form.rb
steps %w[first second third]
def self.steps_details
{
first: 'Credentials',
second: 'Personal Details',
third: 'Contact Information'
}
end
📘 How to customize steps titles and descriptions
attribute :username, :string
attribute :password, :string
attribute :date_of_birth, :date
attribute :gender, :string
attribute :favourite_color, :string
attribute :device_type, :string
attribute :email, :string
attribute :phone_number, :string
attribute :country, :string
attribute :city, :string
attribute :address_field_1, :string
attribute :address_field_2, :string
attribute :zip_code, :string
📘 Different types and how to add custom types such as Array
and hash
explained.
with_options if: :first? do |step|
step.validates :username, presence: true, length: 3..10
step.validate :custom_username_validation
...
end
with_options if: :second? do |step|
step.validates :date_of_birth, presence: true
step.validate :custom_date_of_birth_validation
...
end
attr_reader :identifier
def save!
user.save!(validate: false)
personal_detail.save!(validate: false)
contact_info.save!(validate: false)
@identifier = user.id
end
📘 Check more complex examples and how to read them on the controller.
-
Scaffolding will generate example structure of view files:
- show.html.erb
- finish.html.erb
- _wizard.html.erb
- _form_errors.html.erb
and steps/:
- _first.html.erb
- _second.html.erb
- _third.html.erb
❗ Please notice that show and finish are action views, others are partials.
🎨 Feel free to modify html and styles around the form.
By default Scaffolding generates 3 steps, but you can modify, delete or add new steps.
Just make sure that steps are _partials and match corresponded names inside Form (e.g. CatgirlsSurveyForm):
# app/views/catgirls_survey/steps/_first.html.erb
<%= form.label :username %>
<%= form.text_field :username %>
<br>
<%= form.label :password %>
<%= form.text_field :password %>
e.g. CatgirlsSurveyController - app/controllers/catgirls_survey_controller.rb
Fetch resource(s) from DB using identifier
, which you set in .save!
def finish
@record = User.find_by(id: params[:identifier])
end
📘 Check more complex examples and how to read them on the controller.
Bug reports and pull requests are welcome on GitHub at https://github.com/vergilet/schoolgirl_uniform
Feel free to contribute:
- Fork it (https://github.com/vergilet/schoolgirl_uniform/fork)
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
The gem is available as open source under the terms of the MIT License.
Copyright © 2016 Yaro.