Skip to content

liangzan/formtastic_validation

Repository files navigation

formtastic_validation

Introduction

Formtastic validation adds client side validation to your forms. Visit the demo app and its repo to see it in action.

Features

  • works with existing formtastic code. no need to modify anything
  • javascript lib agnostic.
  • no AJAX that pounds your servers. its completely client side javascript
  • unobtrusive and very light weight

Dependencies

Installation

  $ gem install formtastic_validation

Remember to add the gem into your bundler Gemfile

Then run the generators to add the files on

From your rails root directory:

  $ rails generate formtastic_validation

This adds a configuration file to config/initializers and a couple of javascript files to public/javascripts

After that you need to add this to your application layout

  <head>
    <%= javascript_include_tag 'livevalidation-1.3.min' %>
    <%= javascript_include_tag 'formtastic_validation-0.1.4.min' %>
    <script type="text/javascript">
      function initialize() {
          var formtasticValidation = new FormtasticValidation;
          formtasticValidation.initialize();
      }
    </script>
  </head>
  <body onload="initialize()">
  </body>

What the above does:

  • it included the livevalidation and formtastic_validation javascript files
  • it ran a function that instantiates a FormatasticValidation object and let it call initialize()

Usage

There is nothing to change in your existing code. You should see “Thankyou!” appearing next to fields which passed the validations. Forms which do not fufil the validations will fail to submit.

Validation options work.

Configuration

The valid message can be changed in the formtastic_validation configuration file under config/initializers

CSS Styling

You can style the validation messages with the css selectors

.LV_validation_message { ... }   /* the validation message */
.LV_valid { ... }                /* for valid messages */
.LV_invalid { ... }              /* for invalid messages */

Compatibility

This gem is Rails 2 and 3 compatible.

Upgrading

Existing users need not change anything from the upgrade from rails 2 to 3.

Caveats

Only the following validations work:

  1. validates_presence_of
  2. validates_acceptance_of
  3. validates_confirmation_of
  4. validates_exclusion_of
  5. validates_inclusion_of
  6. validates_numericality_of
  7. validates_format_of
  8. validates_length_of

validates_size_of does not work. Do use my fork of the validation_reflection gem for it to work.

The rest do not work as they depend on server queries.

Alternative implementations

http://github.com/dnclabs/client_side_validations
http://github.com/grimen/validatious-on-rails

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a
    future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Wong Liang Zan. See LICENSE for details.

About

validate form fields on the fly with formtastic

Resources

License

Stars

Watchers

Forks

Packages

No packages published