Commit fbfbd6a8bf0033da30f6f929ba686dba36d9801f

Authored by Daniela Feitosa
1 parent 8b24632e

Forms: Warning users about blank mandatory fields

(ActionItem2681)
plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb
... ... @@ -27,6 +27,7 @@ class CustomFormsPluginProfileController < ProfileController
27 27 failed_answers.each do |answer|
28 28 @submission.errors.add(answer.field.name.to_sym, answer.errors[answer.field.slug.to_sym])
29 29 end
  30 + raise 'Submission failed: answers not valid'
30 31 end
31 32 session[:notice] = _('Submission saved')
32 33 redirect_to :action => 'show'
... ...
plugins/custom_forms/lib/custom_forms_plugin/helper.rb
... ... @@ -103,14 +103,17 @@ module CustomFormsPlugin::Helper
103 103  
104 104 def build_answers(submission, form)
105 105 answers = []
106   - submission.each do |slug, value|
107   - field = form.fields.select {|field| field.slug==slug}.first
108   - if value.kind_of?(String)
109   - final_value = value
110   - elsif value.kind_of?(Array)
111   - final_value = value.join(',')
112   - elsif value.kind_of?(Hash)
113   - final_value = value.map {|option, present| present == '1' ? option : nil}.compact.join(',')
  106 + form.fields.each do |field|
  107 + final_value = ''
  108 + if submission.has_key?(field.slug)
  109 + value = submission[field.slug]
  110 + if value.kind_of?(String)
  111 + final_value = value
  112 + elsif value.kind_of?(Array)
  113 + final_value = value.join(',')
  114 + elsif value.kind_of?(Hash)
  115 + final_value = value.map {|option, present| present == '1' ? option : nil}.compact.join(',')
  116 + end
114 117 end
115 118 answers << CustomFormsPlugin::Answer.new(:field => field, :value => final_value)
116 119 end
... ...
plugins/custom_forms/test/functional/custom_forms_plugin_profile_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/custom_forms_plugin_profile_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class CustomFormsPluginProfileController; def rescue_action(e) raise e end; end
  6 +
  7 +class CustomFormsPluginProfileControllerTest < ActionController::TestCase
  8 + def setup
  9 + @controller = CustomFormsPluginProfileController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + @profile = create_user('profile').person
  13 + login_as(@profile.identifier)
  14 + environment = Environment.default
  15 + environment.enable_plugin(CustomFormsPlugin)
  16 + end
  17 +
  18 + attr_reader :profile
  19 +
  20 + should 'save submission if fields are ok' do
  21 + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  22 + field1 = CustomFormsPlugin::TextField.create(:name => 'Name', :form => form, :mandatory => true)
  23 + field2 = CustomFormsPlugin::TextField.create(:name => 'License', :form => form)
  24 +
  25 + assert_difference CustomFormsPlugin::Submission, :count, 1 do
  26 + post :show, :profile => profile.identifier, :id => form.id, :submission => {field1.name.to_slug => 'Noosfero', field2.name.to_slug => 'GPL'}
  27 + end
  28 + assert !session[:notice].include?('not saved')
  29 + assert_redirected_to :action => 'show'
  30 + end
  31 +end
... ...