Skip to content
Browse files

maxlength validator now supports text_area

  • Loading branch information...
1 parent 61c38aa commit 07b65a2bcdae30d62d513d5c1c954813441dd723 @ursm ursm committed Jun 14, 2012
Showing with 48 additions and 10 deletions.
  1. +1 −0 README.rdoc
  2. +18 −2 lib/html5_validators/action_view/form_helpers.rb
  3. +7 −1 spec/fake_app.rb
  4. +22 −7 spec/requests/validation_spec.rb
View
1 README.rdoc
@@ -41,6 +41,7 @@ http://img.skitch.com/20110517-8sagqrkjnmkinapmcc5tduy2b8.jpg
View:
<%= f.text_field :name %>
+ text_area is also available
HTML:
<input id="user_name" maxlength="10" name="user[name]" size="10" type="text" />
View
20 lib/html5_validators/action_view/form_helpers.rb
@@ -5,6 +5,12 @@ def inject_required_field
@options["required"] ||= object.class.attribute_required?(@method_name)
end
end
+
+ def inject_maxlength_field
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
+ @options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
+ end
+ end
end
end if ActionPack::VERSION::STRING >= '4'
@@ -28,9 +34,9 @@ class Base #:nodoc:
class TextField
def render_with_html5_attributes
inject_required_field
+ inject_maxlength_field
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
- @options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
@options["max"] ||= object.class.attribute_max(@method_name)
@options["min"] ||= object.class.attribute_min(@method_name)
end
@@ -39,8 +45,17 @@ def render_with_html5_attributes
alias_method_chain :render, :html5_attributes
end
+ class TextArea
+ def render_with_html5_attributes
+ inject_required_field
+ inject_maxlength_field
+
+ render_without_html5_attributes
+ end
+ end
+
#TODO probably I have to add some more classes here
- [TextArea, RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
+ [RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
kls.class_eval do
def render_with_html5_attributes
inject_required_field
@@ -67,6 +82,7 @@ def to_input_field_tag_with_html5_attributes(field_type, options = {})
def to_text_area_tag_with_html5_attributes(options = {})
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
+ options["maxlength"] ||= object.class.attribute_maxlength(method_name)
end
to_text_area_tag_without_html5_attributes options
end
View
8 spec/fake_app.rb
@@ -31,6 +31,7 @@ def new
render :inline => <<-ERB
<%= form_for @person do |f| %>
<%= f.text_field :name %>
+<%= f.text_area :bio %>
<% end %>
ERB
end
@@ -52,6 +53,11 @@ module ApplicationHelper; end
#migrations
class CreateAllTables < ActiveRecord::Migration
def self.up
- create_table(:people) {|t| t.string :name; t.string :email; t.integer :age}
+ create_table :people do |t|
+ t.string :name
+ t.string :email
+ t.integer :age
+ t.text :bio
+ end
end
end
View
29 spec/requests/validation_spec.rb
@@ -17,20 +17,21 @@
context 'with required validation' do
background do
- Person.validates_presence_of :name
+ Person.validates_presence_of :name, :bio
end
after do
Person._validators.clear
end
scenario 'new form' do
visit '/people/new'
- page.should have_css('input#person_name')
- page.should have_css('input#person_name[required=required]')
+
+ find('input#person_name')[:required].should == 'required'
+ find('textarea#person_bio')[:required].should == 'required'
end
scenario 'new_without_html5_validation form' do
visit '/people/new_without_html5_validation'
- page.should have_css('input#person_name')
- page.should_not have_css('input#person_name[required=required]')
+
+ find('input#person_name')[:required].should be_nil
end
context 'disabling html5_validation in class level' do
@@ -46,9 +47,23 @@
end
scenario 'new form' do
visit '/people/new'
- page.should have_css('input#person_name')
- page.should_not have_css('input#person_name[required=required]')
+
+ find('input#person_name')[:required].should be_nil
end
end
end
+
+ context 'with maxlength validation' do
+ background do
+ Person.validates_length_of :name, {:maximum => 20}
+ Person.validates_length_of :bio, {:maximum => 100}
+ end
+
+ scenario 'new form' do
+ visit '/people/new'
+
+ find('input#person_name')[:maxlength].should == '20'
+ find('textarea#person_bio')[:maxlength].should == '100'
+ end
+ end
end

0 comments on commit 07b65a2

Please sign in to comment.
Something went wrong with that request. Please try again.