Commit bfc0d0edbe592962d2bcdc2923060cc7cb04bc90
1 parent
e66ee1cd
Exists in
master
and in
29 other branches
Prevents new forms to be saved with select field without alternatives
I had to put the validation for the length of alternatives in the Field model because, since we are using Single Table Inheritance, we are creating Field objects with the type as a param instead of creating SelectField objects. This way, subclass' validation are skipped. I kept the validation in the SelectField model as well, because we also edit existing SelectField objects. (ActionItem2960)
Showing
2 changed files
with
12 additions
and
0 deletions
Show diff stats
plugins/custom_forms/lib/custom_forms_plugin/field.rb
... | ... | @@ -8,6 +8,10 @@ class CustomFormsPlugin::Field < ActiveRecord::Base |
8 | 8 | |
9 | 9 | has_many :alternatives, :order => 'position', :class_name => 'CustomFormsPlugin::Alternative' |
10 | 10 | accepts_nested_attributes_for :alternatives, :allow_destroy => true |
11 | + #FIXME This validation should be in the subclass, but since we are using Single Table | |
12 | + # Inheritance we are instantiating a Field object with the type as a param. So the validation | |
13 | + # had to go here or rails would skip it. | |
14 | + validates_length_of :alternatives, :minimum => 1, :message => 'can\'t be empty', :if => Proc.new { |f| f.type == 'CustomFormsPlugin::SelectField' } | |
11 | 15 | |
12 | 16 | before_validation do |field| |
13 | 17 | field.slug = field.name.to_slug if field.name.present? | ... | ... |
plugins/custom_forms/test/unit/custom_forms_plugin/field_test.rb
... | ... | @@ -33,6 +33,14 @@ class CustomFormsPlugin::FieldTest < ActiveSupport::TestCase |
33 | 33 | assert_equal form.fields, [license_field] |
34 | 34 | end |
35 | 35 | |
36 | + should 'have alternative if type is SelectField' do | |
37 | + select = CustomFormsPlugin::Field.new(:name => 'select_field001', :type => 'CustomFormsPlugin::SelectField') | |
38 | + assert !select.save | |
39 | + | |
40 | + select.alternatives << CustomFormsPlugin::Alternative.new(:label => 'option') | |
41 | + assert select.save | |
42 | + end | |
43 | + | |
36 | 44 | should 'sort alternatives by position' do |
37 | 45 | field = CustomFormsPlugin::Field.create!(:name => 'field001') |
38 | 46 | second = CustomFormsPlugin::Alternative.create!(:label => 'second', :field => field, :position => 2) | ... | ... |