Schoolgirl Uniform
πΎ 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.
Installation
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.
Usage and Config
To achieve working multistep form you need to configure FVC:
-
π Form
-
π View
-
π Controller
π Form
e.g. CatgirlsSurveyForm - app/forms/catgirls_survey_form.rb
1. Declare the steps and details if needed in your form:
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
2. Define form fields:
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.
3. Use block validations with step condition to group needed checks:
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
4. Inside save!
method build your records, set them with form attributes and save.
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.
π View
-
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.
βΎοΈ Steps
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 %>
π Controller
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.
Contributing
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
License
The gem is available as open source under the terms of the MIT License.
Copyright Β© 2016 Yaro.