Skip to content

Commit

Permalink
Merge pull request #2637 from bitzesty/matrix-validation
Browse files Browse the repository at this point in the history
Only allow whole numbers for matrix question inputs
  • Loading branch information
TheDancingClown authored Oct 18, 2023
2 parents 262b4d9 + 39d299c commit 00cf850
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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

0 comments on commit 00cf850

Please sign in to comment.