Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix survey validation #228

Merged
merged 10 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions OVERLOADS.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ end
* `spec/mailers/decidim/budgets/vote_reminder_mailer_spec.rb`
* `spec/services/decidim/budgets/order_reminder_generator_spec.rb`
* `spec/system/admin_reminds_users_with_pending_orders_spec.rb`

## Fix survey validation
* `app/cells/decidim/forms/step_navigation/show.erb`
* `app/packs/src/decidim/decidim_application.js`
* `app/views/decidim/forms/questionnaires/show.html.erb`
* `config/initializers/decidim_verifications.rb`
* `spec/shared/has_questionnaire.rb`
* `spec/system/survey_spec.rb`
37 changes: 37 additions & 0 deletions app/cells/decidim/forms/step_navigation/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div class="button--double form-general-submit answer-questionnaire__submit">
<% if first_step? || errors %>
<a></a>
<% else %>
<%= link_to(
icon("caret-left", class: "icon--small", role: "img", "aria-hidden": true) + " " + t("decidim.forms.step_navigation.show.back"),
"#",
class: "hollow secondary back_survey",
data: {
toggle: [previous_step_dom_id, current_step_dom_id].join(" ")
}
) %>
<% end %>

<% if last_step? %>
<%= form.submit(
t("decidim.forms.step_navigation.show.submit"),
class: "button button--sc submit_survey",
disabled: button_disabled?,
data: {
confirm: t("decidim.forms.step_navigation.show.are_you_sure"),
disable: true
}
) %>
<% elsif errors %>
<a></a>
<% else %>
<%= link_to(
t("decidim.forms.step_navigation.show.continue"),
"#",
class: "button button--sc next_survey",
data: {
toggle: [next_step_dom_id, current_step_dom_id].join(" ")
}
) %>
<% end %>
</div>
35 changes: 35 additions & 0 deletions app/packs/src/decidim/decidim_application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@

// Load images
require.context("../../images", true)

import $ from "jquery"
import "jquery-validation"

$(() => {
if($(".submit_survey").length) {
$("body").on('DOMNodeInserted', '.confirm-reveal', function () {
$(".confirm-reveal .button:first").on("mouseup", function () {
$("form.answer-questionnaire").validate({
ignore: "thrhwrt",
errorPlacement: function (error, element) {},
focusInvalid: false,
invalidHandler: function(form, validator) {

$(".questionnaire-step").each(function () {
$(this).removeClass("hide");
});
$(".next_survey").hide();
$(".back_survey").hide();

if (!validator.numberOfInvalids())
return;

const y = validator.errorList[0].element.parentElement.getBoundingClientRect().top + window.pageYOffset - 10;

window.scrollTo({top: y, behavior: 'smooth'});
}
});
if ($("form.answer-questionnaire").valid()) {
$(".survey-form").submit();
}
});
});
}
});
155 changes: 155 additions & 0 deletions app/views/decidim/forms/questionnaires/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<% add_decidim_meta_tags({
title: translated_attribute(questionnaire.title),
description: translated_attribute(questionnaire.description),
}) %>

<% columns = allow_answers? && visitor_can_answer? && @form.responses.map(&:question).any?(&:matrix?) ? 9 : 6 %>

<%= render partial: "decidim/shared/component_announcement" if current_component.manifest_name == "surveys" %>

<div class="row columns">
<h2 class="section-heading"><%= translated_attribute questionnaire.title %></h2>
<div class="row">
<div class="columns large-<%= columns %> medium-centered lead">
<%= decidim_sanitize_editor translated_attribute questionnaire.description %>
</div>
</div>
</div>

<div class="row">
<div class="columns large-<%= columns %> medium-centered">
<div class="card">
<div class="card__content">
<% unless questionnaire_for.try(:component)&.try(:published?) %>
<div class="section">
<div class="callout warning">
<p><%= t(".questionnaire_not_published.body") %></p>
</div>
</div>
<% end %>

<% if allow_answers? %>
<% if visitor_can_answer? %>
<% if visitor_already_answered? %>
<div class="section">
<div class="callout success">
<h3 class="heading5"><%= t(".questionnaire_answered.title") %></h3>
<p><%= t(".questionnaire_answered.body") %></p>
</div>
</div>
<% else %>
<div class="answer-questionnaire">
<noscript>
<div class="section">
<div class="callout warning">
<h3 class="heading5"><%= t(".questionnaire_js_disabled.title") %></h3>
<p><%= t(".questionnaire_js_disabled.body") %></p>
</div>
</div>
</noscript>
<% unless current_participatory_space.can_participate?(current_user) %>
<div class="section">
<div class="callout alert">
<h3 class="heading5"><%= t(".questionnaire_for_private_users.title") %></h3>
<p><%= t(".questionnaire_for_private_users.body") %></p>
</div>
</div>
<% end %>

<%= decidim_form_for(@form, url: update_url, method: :post, html: { multipart: true, class: "form answer-questionnaire" }, data: { "safe-path" => form_path }) do |form| %>
<%= form_required_explanation %>
<%= invisible_captcha %>
<% answer_idx = 0 %>
<% cleaned_answer_idx = 0 %>
<% @form.responses_by_step.each_with_index do |step_answers, step_index| %>
<div id="step-<%= step_index %>" class="<%= (step_index.zero? || invalid?(@form.responses)) ? "questionnaire-step" : "questionnaire-step hide" %>" data-toggler=".hide">
<% if @form.total_steps > 1 %>
<h3 class="section-heading">
<%= t(".current_step", step: step_index + 1) %>
<span class="answer-questionnaire__steps"><%= t(".of_total_steps", total_steps: @form.total_steps) %></span>
</h3>
<% end %>

<% step_answers.each do |answer| %>
<div class="row column answer question" data-max-choices="<%= answer.question.max_choices %>" data-conditioned="<%= answer.question.display_conditions.any? %>" data-question-id="<%= answer.question.id %>">
<% answer.question.display_conditions.each do |display_condition| %>
<%= content_tag :div, nil, class: "display-condition", data: display_condition.to_html_data %>
<% end %>

<%= fields_for "questionnaire[responses][#{answer_idx}]", answer do |answer_form| %>
<%= render(
"decidim/forms/questionnaires/answer",
answer_form: answer_form,
answer: answer,
answer_idx: answer_idx,
cleaned_answer_idx: cleaned_answer_idx,
disabled: !current_participatory_space.can_participate?(current_user)
) %>
<% end %>
</div>
<% if !(answer.question.separator? || answer.question.title_and_description?) %>
<% cleaned_answer_idx += 1 %>
<% end %>
<% answer_idx += 1 %>
<% end %>

<% if step_index + 1 == @form.total_steps %>
<% if show_represent_user_group? %>
<div class="row column represent-user-group">
<%= cell("decidim/represent_user_group", form) %>
</div>
<% end %>

<% if show_public_participation? %>
<div class="row column public-participation">
<%= cell("decidim/public_participation", form) %>
</div>
<% end %>

<div class="row column tos-agreement">
<%= form.check_box :tos_agreement, label: t(".tos_agreement"), id: "questionnaire_tos_agreement", disabled: !current_participatory_space.can_participate?(current_user) %>
<div class="help-text">
<%= decidim_sanitize_editor translated_attribute questionnaire.tos %>
</div>
</div>
<% end %>

<%= cell(
"decidim/forms/step_navigation",
step_index,
total_steps: @form.total_steps,
button_disabled: !current_participatory_space.can_participate?(current_user),
form: form,
errors: invalid?(@form.responses)
) %>
</div>
<% end %>
<% end %>
</div>
<% end %>
<% else %>
<div class="answer-questionnaire">
<h3 class="section-heading"><%= t(".answer_questionnaire.title") %></h3>
<p>
<%= t(".answer_questionnaire.anonymous_user_message", sign_in_link: decidim.new_user_session_path, sign_up_link: decidim.new_user_registration_path).html_safe %>
</p>

<ol>
<%= cell("decidim/forms/question_readonly", collection: @questionnaire.questions.not_conditioned) %>
</ol>
</div>
<% end %>
<% else %>
<div class="section">
<div class="callout warning">
<h3 class="heading4"><%= t(".questionnaire_closed.title") %></h3>
<p><%= t(".questionnaire_closed.body") %></p>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>

<%= javascript_pack_tag "decidim_forms" %>
3 changes: 3 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ ignore_missing:
- layouts.decidim.header.user_menu
- decidim.devise.shared.omniauth_buttons.or
- devise.shared.links.sign_in_with_provider
- decidim.forms.step_navigation.*
- decidim.forms.questionnaires.show.*
- decidim.account.destroy.success
- decidim.account.destroy.error

# Consider these keys used:
ignore_unused:
- faker.*
Expand Down
16 changes: 14 additions & 2 deletions config/initializers/decidim_verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
# end
# end

Decidim::Verifications.register_workflow(:osp_authorization_handler) do |auth|
auth.form = "Decidim::OspAuthorizationHandler"
if Rails.env.test?
Decidim::Verifications.register_workflow(:dummy_authorization_handler) do |workflow|
workflow.form = "DummyAuthorizationHandler"
workflow.action_authorizer = "DummyAuthorizationHandler::DummyActionAuthorizer"
workflow.expires_in = 1.hour

workflow.options do |options|
options.attribute :postal_code, type: :string, default: "08001", required: false
end
end
else
Decidim::Verifications.register_workflow(:osp_authorization_handler) do |auth|
auth.form = "Decidim::OspAuthorizationHandler"
end
end
2 changes: 2 additions & 0 deletions config/initializers/extends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

require "extends/controllers/decidim/devise/account_controller_extends"
require "extends/controllers/decidim/meetings/meetings_controller_extends"
require "extends/cells/decidim/forms/step_navigation_cell_extends"
require "extends/helpers/decidim/forms/application_helper_extends"
require "extends/cells/decidim/budgets/project_list_item_cell_extends"
15 changes: 15 additions & 0 deletions lib/extends/cells/decidim/forms/step_navigation_cell_extends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Decidim
module Forms
module StepNavigationCellExtends
def errors
options[:errors]
end
end
end
end

Decidim::Forms::StepNavigationCell.class_eval do
prepend(Decidim::Forms::StepNavigationCellExtends)
end
15 changes: 15 additions & 0 deletions lib/extends/helpers/decidim/forms/application_helper_extends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Decidim
module Forms
module ApplicationHelperExtends
def invalid?(responses)
responses.map { |response| response.errors.any? }.any?
end
end
end
end

Decidim::Forms::ApplicationHelper.module_eval do
prepend(Decidim::Forms::ApplicationHelperExtends)
end
28 changes: 22 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"highlight.js": "^11.6.0",
"inline-attachment": "^2.0.3",
"inscrybmde": "^1.11.6",
"jquery": "^3.6.3",
"jquery-ui": "^1.13.2",
"jquery-validation": "^1.19.5",
"jsrender": "^1.0.11",
"leaflet": "^1.3.4",
"leaflet.featuregroup.subgroup": "^1.0.2",
Expand Down
2 changes: 2 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
require "decidim/participatory_processes/test/factories"
# require "decidim/decidim_awesome/test/factories"
require "decidim/verifications/test/factories"
require "decidim/forms/test/factories"
require "decidim/surveys/test/factories"
Loading