From fbfbd6a8bf0033da30f6f929ba686dba36d9801f Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Wed, 29 May 2013 04:11:35 +0000 Subject: [PATCH] Forms: Warning users about blank mandatory fields --- plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb | 1 + plugins/custom_forms/lib/custom_forms_plugin/helper.rb | 19 +++++++++++-------- plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb index 61dcbdf..6bc0142 100644 --- a/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb +++ b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb @@ -27,6 +27,7 @@ class CustomFormsPluginProfileController < ProfileController failed_answers.each do |answer| @submission.errors.add(answer.field.name.to_sym, answer.errors[answer.field.slug.to_sym]) end + raise 'Submission failed: answers not valid' end session[:notice] = _('Submission saved') redirect_to :action => 'show' diff --git a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb index e3929d8..27948ad 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/helper.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/helper.rb @@ -103,14 +103,17 @@ module CustomFormsPlugin::Helper def build_answers(submission, form) answers = [] - submission.each do |slug, value| - field = form.fields.select {|field| field.slug==slug}.first - if value.kind_of?(String) - final_value = value - elsif value.kind_of?(Array) - final_value = value.join(',') - elsif value.kind_of?(Hash) - final_value = value.map {|option, present| present == '1' ? option : nil}.compact.join(',') + form.fields.each do |field| + final_value = '' + if submission.has_key?(field.slug) + value = submission[field.slug] + if value.kind_of?(String) + final_value = value + elsif value.kind_of?(Array) + final_value = value.join(',') + elsif value.kind_of?(Hash) + final_value = value.map {|option, present| present == '1' ? option : nil}.compact.join(',') + end end answers << CustomFormsPlugin::Answer.new(:field => field, :value => final_value) end diff --git a/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb b/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb new file mode 100644 index 0000000..78ac5c0 --- /dev/null +++ b/plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb @@ -0,0 +1,31 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/custom_forms_plugin_profile_controller' + +# Re-raise errors caught by the controller. +class CustomFormsPluginProfileController; def rescue_action(e) raise e end; end + +class CustomFormsPluginProfileControllerTest < ActionController::TestCase + def setup + @controller = CustomFormsPluginProfileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @profile = create_user('profile').person + login_as(@profile.identifier) + environment = Environment.default + environment.enable_plugin(CustomFormsPlugin) + end + + attr_reader :profile + + should 'save submission if fields are ok' do + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') + field1 = CustomFormsPlugin::TextField.create(:name => 'Name', :form => form, :mandatory => true) + field2 = CustomFormsPlugin::TextField.create(:name => 'License', :form => form) + + assert_difference CustomFormsPlugin::Submission, :count, 1 do + post :show, :profile => profile.identifier, :id => form.id, :submission => {field1.name.to_slug => 'Noosfero', field2.name.to_slug => 'GPL'} + end + assert !session[:notice].include?('not saved') + assert_redirected_to :action => 'show' + end +end -- libgit2 0.21.2