Skip to content

Commit

Permalink
added support for required fields
Browse files Browse the repository at this point in the history
added html5 required attribute, as well as additional required class on input wrapper for better styling support
  • Loading branch information
brewster1134 committed Aug 11, 2012
1 parent cb7002a commit 6e56341
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ tmp/**/*
.idea/*.xml
.sass-cache
nbproject
.DS_Store
14 changes: 10 additions & 4 deletions app/assets/stylesheets/rails_admin/base/theming.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ body.rails_admin {
.control-group .hidden_type {
display:none;
}

legend {
cursor:pointer;
i {
vertical-align: inherit !important;
}
}

&.denser {
.controls .nav {
margin-bottom:5px;
Expand All @@ -67,6 +67,12 @@ body.rails_admin {
.control-group {
margin: 0px;
padding-top:15px;

&.required {
.control-label {
font-weight:bold;
}
}
}
.control-label {
width:145px;
Expand All @@ -75,7 +81,7 @@ body.rails_admin {
margin-bottom:0px;
}
}

/* We want input size to be used, unfixate input width */
input, textarea {
width:auto;
Expand All @@ -95,7 +101,7 @@ body.rails_admin {
&:hover>.remove_nested_fields {
display:block;
}

border-left:5px solid $blue;
padding-left:5px;
fieldset {
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/rails_admin/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def generate(options = {})
if options[:nested_in]
action = :nested
elsif @template.request.format == 'text/javascript'
action = :modal
action = :modal
else
action = options[:action]
end

groups = options[:model_config].send(action).with(:form => self, :object => @object, :view => @template).visible_groups

object_infos +
Expand All @@ -45,13 +45,13 @@ def field_wrapper_for field, nested_in
# do not show nested field if the target is the origin
unless field.inverse_of.presence && field.inverse_of == nested_in &&
@template.instance_variable_get(:@model_config).abstract_model == field.associated_model_config.abstract_model
@template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", :id => "#{dom_id(field)}_field") do
@template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?} #{'required' if field.required?}", :id => "#{dom_id(field)}_field") do
label(field.method_name, field.label, :class => 'control-label') +
(field.nested_form ? field_for(field) : input_for(field))
end
end
else
(field.nested_form ? field_for(field) : input_for(field))
(field.nested_form ? field_for(field) : input_for(field))
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class String < RailsAdmin::Config::Fields::Base
register_instance_option(:html_attributes) do
{
:maxlength => length,
:size => [50, length.to_i].min
:size => [50, length.to_i].min,
:required => required?
}
end

Expand Down
8 changes: 6 additions & 2 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ class HelpTest < Tableless
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length of 1-49.")
end

end

it "should have accessor for its fields" do
Expand Down Expand Up @@ -330,6 +329,12 @@ class HelpTest < Tableless
should have_selector("label", :text => "Manager (STRING)")
should have_selector("label", :text => "Ballpark (STRING)")
end

it 'should be required' do
visit new_path(:model_name => "team")
should have_selector '.required#team_manager_field'
find('.required#team_manager_field input#team_manager')['required'].should_not be_blank
end
end

describe "items' fields" do
Expand Down Expand Up @@ -889,7 +894,6 @@ class HelpTest < Tableless
end
end


describe "fields which are nullable and have AR validations", :active_record => true do

it "should be required" do
Expand Down

0 comments on commit 6e56341

Please sign in to comment.