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:
=== 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[http://github.com/grosser/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:
We found that pluginaweek-smart_field_constraints demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 10 Aug 2014
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Malicious Go packages are impersonating popular libraries to install hidden loader malware on Linux and macOS, targeting developers with obfuscated payloads.
Bybit's $1.46B hack by North Korea's Lazarus Group pushes 2025 crypto losses to $1.6B in just two months, already surpassing all of 2024's $1.49B total.
OpenSSF has published OSPS Baseline, an initiative designed to establish a minimum set of security-related best practices for open source software projects.