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

Only allow whole numbers for matrix question inputs #2637

Merged
merged 2 commits into from
Oct 18, 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
4 changes: 4 additions & 0 deletions app/assets/javascripts/frontend/matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).on("input", ".matrix-question-input", function() {
// allow only numbers on input
this.value = this.value.replace(/\D/g,'');
});
2 changes: 1 addition & 1 deletion app/views/qae_form/_matrix_question.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ table.matrix-question-table.govuk-table class="#{'auto-totals-column' if questio
]
span.govuk-error-message aria-live="polite"
- if @form_answer.validator_errors && @form_answer.validator_errors["#{question.hash_key}_#{x_heading.key}_#{y_heading.key}"].present?
| Required
= @form_answer.validator_errors["#{question.hash_key}_#{x_heading.key}_#{y_heading.key}"]
span.govuk-visually-hidden id="error_for_#{question.key}"
| Error:
=< @form_answer.validator_errors[question.hash_key]
15 changes: 11 additions & 4 deletions forms/qae_form_builder/matrix_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ def errors
question.y_headings.each do |y_heading|
question.x_headings.each do |x_heading|
suffix = "#{x_heading.key}_#{y_heading.key}"
if !question.input_value(suffix: suffix).present? && !AUTO_CALCULATED_HEADINGS.any? { |excluded| question.input_name(suffix: suffix).include?(excluded) }
if (question.required_row_parent && question.required_rows.include?(y_heading.key)) || !question.required_row_parent
result[question.hash_key(suffix: suffix)] ||= ""
result[question.hash_key(suffix: suffix)] << "Required"
if !AUTO_CALCULATED_HEADINGS.any? { |excluded| question.input_name(suffix: suffix).include?(excluded) }
if !question.input_value(suffix: suffix).present?
if (question.required_row_parent && question.required_rows.include?(y_heading.key)) || !question.required_row_parent
result[question.hash_key(suffix: suffix)] ||= ""
result[question.hash_key(suffix: suffix)] << "Required"
end
else
if !/\A\d+\z/.match(question.input_value(suffix: suffix).to_s)
result[question.hash_key(suffix: suffix)] ||= ""
result[question.hash_key(suffix: suffix)] << "Must be a whole number"
end
end
end
end
Expand Down
Loading