0.0
No commit activity in last 3 years
No release in over 3 years
Ajax Validator is a Rails engine that allows to validate form fields.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Project Readme

Ajax Validator

Ajax Live Form Validation Rails Engine

Gem Version Build Status

Demo

How to install

  1. Add to Gemfile

     gem 'ajax_validator'
    
  2. Bundle

     bundle install
    
  3. Create file config/initializers/ajax_validator.rb and specify validatable models

     AjaxValidator::Engine.setup do |config|
       config.ajax_validator_engine = {
         # if form object is used, both classes must be put here
         whitelist_klasses: ['some_klass', 'some_module/some_klass']
       }
     end
    
  4. Copy migration (currently, not needed)

     bundle exec rake ajax_validator:install:migrations
    
  5. Run migration (currently, not needed)

     bundle exec rake db:migrate SCOPE=ajax_validator
    
  6. Run seeds (currently, not needed)

     bundle exec rake ajax_validator:db:seed
    
  7. Add to config/routes.rb

     mount AjaxValidator::Engine, at: '/ajax_validator'
    

How to use helpers

Include the needed javascript file in your application.js or application.js.coffee

    Javascript
    //= require ajax_validator/application

    or CoffeeScript
    #= require ajax_validator/application

    Script will add class 'has-error' or 'has-success' to the field wrapping div, based on the result.

    Add these attributes on text input field:
        class: 'ajax_validator__field'
        data-form-object='validations/signup_form_company_name' [Optional, leave empty string '' to validate on model directly]
        data-resource-model='company'
        data-resource-attr-name='name'
        data-ignore-resource-attr-value='false' [Optional, set to 'true' when attribute is assembled from additional params instead]
        data-resource-instance-additional-params='{"something": true, "something_else": ""}' [Optional]

    Example on Bootstrap:

    ERB:
    <div class="input-group">
        <input class='form-control ajax_validator__field' data-form-object='validations/signup_form_company_name' data-resource-model='company' data-resource-attr-name='name' type='text' name='signup_form[company]' id='signup_form_company'>
    </div>

    or HAML:
    .input-group
        = f.text_field :company, class: 'form-control ajax_validator__field', data: {form_object: 'validations/signup_form_company_name', resource_model: 'company', resource_attr_name: 'name'}

How to add a new gem to the engine

  • Add a gem to Gemfile
  • Run: bundle
  • Copy over to multiple gemfiles: bundle exec appraisal install

How to test

$ sudo nano -w /etc/postgresql/9.3/main/pg_hba.conf
# Database administrative login by Unix domain socket
# local   all             postgres                                trust

$ sudo service postgresql reload
  • Install the latest PhantomJS:
    sudo su
    cd /opt
    wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
    tar xjvf phantomjs-2.1.1-linux-x86_64.tar.bz2
    ln -s phantomjs-2.1.1-linux-x86_64 phantomjs
    ln -s /opt/phantomjs/bin/phantomjs /usr/local/bin
    exit
  • Install gems
    bundle exec appraisal
  • Setup the database
    export DATABASE_URL='postgres://localhost:5432' AJAX_VALIDATOR_DATABASE_USERNAME=postgres AJAX_VALIDATOR_DATABASE_PASSWORD=p
    bundle exec appraisal rails-4.1 rake db:create db:migrate db:seed
  • Run tests
    bundle exec appraisal rake spec

  • Optional:

      bundle exec appraisal rails-4.1 rake db:schema:load RAILS_ENV=test
    

bundle exec appraisal rails-4.1 rake spec

How to develop

gem 'ajax_validator', path: 'vendor/engines/ajax_validator'

bundle exec rails g scaffold ajax_validator/post name:string --integration_tool=rspec --helper=false --assets=false
bundle exec rake db:migrate

cd spec/dummy/
bundle exec rails s