Skip to content

Latest commit

 

History

History
118 lines (87 loc) · 3.58 KB

README.md

File metadata and controls

118 lines (87 loc) · 3.58 KB

ParsleySimpleForm Gem Version Code Climate Bitdeli Badge

This project uses MIT-LICENSE.

Client-side validation helper powered by simple_form and parsley.js

Usage

Use parsley_form_for to automatically add client-side validation to your form.

This code:

<%= parsley_form_for(@user) do |f| %>
  <%= f.input :name %>
  <%= f.input :password %>
  <%= f.input :password_confirmation, equalto: :password %>
<% end %>

Automatically adds required constraint to inputs which are required attributes, and validates equality of password and password_confirmation.

Don't forget to add parsley.js to your project. You can do it by adding the gem parsley-rails to project's Gemfile (we don't recommend this approach) or use a front-end package manager like Bower.

This gem supports all parsley's basic constraints

Required

For required constraint you can let ParsleySimpleForm infer about the constraint (by ActiveRecord validates_presence_of) or explicitly indicate this in the field

<%= f.input :password, required: true %>

Not blank

Check if that value is not blank

<%= f.input :name %>

Min/Max length

Validates minimum or maximum length of the field value string

<%= f.input :password, minlength: 6 %>
<%= f.input :nickname, maxlength: 10 %>

Range length

Check if the field have and string between the range

<%= f.input :password, rangelength: 5..10 %>

Note: You can use a Range object or a string, ie "[5,10]"

Min/Max

Validates the numerical value of a field

<%= f.input :drivers, max: 1 %>
<%= f.input :passengers, min: 0 %>

Range

Validate numerical value between a range

<%= f.input :passengers, range: 1..4 %>

RegExp

Checks with the string matches with a given expression

<%= f.input :twitter_account, regexp: '$@' %>

Equal to

Validates if the value between two fields are identical

<%= f.input :password %>
<%= f.input :password_confirmation, equalto: :password %>

Min/Max/Range check

Validates that a certain minimum/maximum number of checkboxes in a group are checked. You can define a group by giving the same name to radio / checkboxes elements or add a check_group property to each one of them

<%= f.input :active_red, mincheck: 2, check_group: 'leds' %>
<%= f.input :active_green, check_group: 'leds' %>
<%= f.input :active_blue, check_group: 'leds' %>

Or for a range:

<%= f.input :active_red, rangencheck: 1...2, check_group: 'leds' %>
<%= f.input :active_green, check_group: 'leds' %>
<%= f.input :active_blue, check_group: 'leds' %>

I18n

This gem will add custom error messages if properly entries are setted in the locales folder. You can see examples in this folder

Roadmap

  1. Compatible with Parsley.js 2
  2. Don't depend from simple_form
  3. Get i18n messages from ActiveRecord default fields

Contributions

Feel free to make a PR or add an issue to the project :)