Coder Social home page Coder Social logo

pluginaweek / smart_field_constraints Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 2.0 154 KB

Intelligently applies a maxlength attribute for text fields based on column constraints and validations

Home Page: http://wiki.pluginaweek.org/Acts_as_enumeration

License: MIT License

Ruby 100.00%

smart_field_constraints's Introduction

smart_field_constraints

smart_field_constraints intelligently applies a maxlength attribute for text fields based on column constraints and validations.

Resources

API

Bugs

Development

Testing

Source

  • git://github.com/pluginaweek/smart_field_constraints.git

Mailing List

Description

HTML input field restrictions within forms can often help with both validation and improving the user experience. Normally, adding the maxlength configuration option for input fields is not DRY and duplicates data already available in the model or database. This plugin helps make this automatic.

smart_field_constraints looks in two places for determining the maxlength value for an input field:

  • Model validates_length_of/validates_size_of validations

  • Database column definitions (limits specifically)

Model validations will always take preference over database column definitions.

Usage

There’s nothing that you need to change to be able to use this plugin. It’ll just start automatically adding the maxlength values it finds based on the information described above (unless you define that option yourself).

Example

Model:

class User < ActiveRecord::Base
  validates_length_of :login, :maximum => 12
end

View:

text_field(:user, :login)

HTML:

<input id="user_login" maxlength="12" name="user[login]" size="30" type="text" />

Textarea maxlengths

Since the maxlength attribute is not W3C-compliant for textareas, it is not included in the types of fields that will be automatically assigned length constraints. However, you can easily add this yourself by extending the plugin yourself. For an example of this see smart_field_constraints_textarea.

Caveats

Plugin load order

If you have plugins with models that are loaded before smart_field_constraints is loaded, then any length validations defined in those models will not be tracked and automatically used in form fields. To fix this, you can adjust your application’s plugin load order to ensure that smart_field_constraints is loaded first:

config/environment.rb:

Rails::Initializer.run do |config|
  ...
  config.plugins = [:smart_field_constraints, :all]
  ...
end

Testing

Before you can run any tests, the following gem must be installed:

To run against a specific version of Rails:

rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies

  • Rails 2.0 or later

smart_field_constraints's People

Contributors

obrie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

unders satynos

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.