Skip to content

Commit

Permalink
Merge pull request #3117 from bitzesty/pb/add-front-end-phone-number-…
Browse files Browse the repository at this point in the history
…validations

Add front end phone number validations to application form/s
  • Loading branch information
phil-l-brockwell authored Oct 23, 2024
2 parents 56ee6f2 + d7bacaf commit 631018a
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#= require govuk-frontend-3.13.0.min
#= require jquery
#= require google-libphonenumber/dist/libphonenumber.js
#= require jquery_ujs
#= require vendor/file_upload/jquery.ui.widget
#= require vendor/file_upload/jquery.iframe-transport
Expand Down
20 changes: 20 additions & 0 deletions app/assets/javascripts/frontend/form-validation.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,23 @@ window.FormValidation =
@appendMessage(question, "Select a maximum of " + selection_limit)
@addErrorClass(question)

validatePhoneNumber: (question) ->
questionRef = question.attr("data-question_ref")
input = question.find("input[type='tel']").val()
phoneUtil = libphonenumber.PhoneNumberUtil.getInstance()

try parsedInput = phoneUtil.parse(input, 'GB')
catch exception
# libphonenumber throws an exception if we try and parse something like "1" or "abc"
# so we catch the exception and give it something which is parseable but not valid
@logThis(question, "validatePhoneNumber", "#{input} not a parseable phone number")
parsedInput = phoneUtil.parse('01', 'GB')
finally
if !phoneUtil.isValidNumber(parsedInput)
@logThis(question, "validatePhoneNumber", "#{input} not a valid phone number")
@appendMessage(question, "Question #{questionRef} is incomplete. Enter a phone number, like 01635 960 001, 07701 900 982 or +44 808 157 0192.")
@addErrorClass(question)

validateWebsiteUrl: (question) ->
questionRef = question.attr("data-question_ref")
url = question.find("input[type='website_url']").val()
Expand Down Expand Up @@ -984,6 +1001,9 @@ window.FormValidation =
if question.hasClass("sub-fields-word-max")
@validateSubfieldWordLimit(question)

if question.find("input[type='tel']").length
@validatePhoneNumber(question)

if question.find("input[type='website_url']").length
@validateWebsiteUrl(question)

Expand Down
3 changes: 2 additions & 1 deletion app/views/qae_form/_sub_fields_question.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ div role="group" id="#{question.key}"
- sub_field_key = sub_field_block[:key]
- sub_field_title = sub_field_block[:title]
- sub_field_hint = sub_field_block[:hint]
- sub_field_type = sub_field_block[:type]

.govuk-form-group.question-required
label.govuk-label for=question.input_name(suffix: sub_field_key) value=question.input_value(suffix: sub_field_key)
Expand Down Expand Up @@ -46,7 +47,7 @@ div role="group" id="#{question.key}"
- else
input.govuk-input.js-trigger-autosave[
class=klass
type="text"
type=sub_field_type
id=question.input_name(suffix: sub_field_key)
value=question.input_value(suffix: sub_field_key)
name=question.input_name(suffix: sub_field_key)
Expand Down
4 changes: 1 addition & 3 deletions forms/award_years/v2025/innovation/innovation_step2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ def innovation_step2
end

text :org_telephone, "Main telephone number." do
classes "text-words-max"
required
ref "B 6.1"
type "tel"
style "small"
text_words_max 50
end

sub_fields :press_contact_details, "Contact details for press enquiries." do
Expand All @@ -189,7 +187,7 @@ def innovation_step2
{ title: "Title" },
{ first_name: "First name" },
{ last_name: "Last name" },
{ telephone: "Telephone" },
{ telephone: "Telephone", type: "tel" },
{ email: "Email address" },
])
sub_fields_words_max 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,10 @@ def trade_step2
end

text :org_telephone, "Main telephone number." do
classes "text-words-max"
required
ref "B 6.1"
type "tel"
style "small"
text_words_max 50
end

sub_fields :press_contact_details, "Contact details for press enquiries." do
Expand All @@ -184,7 +182,7 @@ def trade_step2
{ title: "Title" },
{ first_name: "First name" },
{ last_name: "Last name" },
{ telephone: "Telephone" },
{ telephone: "Telephone", type: "tel" },
{ email: "Email address" },
])
sub_fields_words_max 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ def mobility_step2
end

text :org_telephone, "Main telephone number." do
classes "text-words-max"
required
ref "B 6.1"
type "tel"
style "small"
text_words_max 50
end

sub_fields :press_contact_details, "Contact details for press enquiries." do
Expand All @@ -181,7 +179,7 @@ def mobility_step2
{ title: "Title" },
{ first_name: "First name" },
{ last_name: "Last name" },
{ telephone: "Telephone" },
{ telephone: "Telephone", type: "tel" },
{ email: "Email address" },
])
sub_fields_words_max 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ def development_step2
end

text :org_telephone, "Main telephone number." do
classes "text-words-max"
required
ref "B 6.1"
type "tel"
style "small"
text_words_max 50
end

sub_fields :press_contact_details, "Contact details for press enquiries." do
Expand All @@ -186,7 +184,7 @@ def development_step2
{ title: "Title" },
{ first_name: "First name" },
{ last_name: "Last name" },
{ telephone: "Telephone" },
{ telephone: "Telephone", type: "tel" },
{ email: "Email address" },
])
sub_fields_words_max 50
Expand Down
2 changes: 1 addition & 1 deletion forms/qae_form_builder/press_contact_details_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def required_sub_fields
{ title: "Title" },
{ first_name: "First name" },
{ last_name: "Last name" },
{ telephone: "Telephone" },
{ telephone: "Telephone", type: "tel" },
{ email: "Email address" },
]
end
Expand Down
2 changes: 1 addition & 1 deletion forms/qae_form_builder/sub_fields_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def required_sub_fields

def rendering_sub_fields
sub_fields.map do |f|
{ key: f.keys.first, title: f.values.first, hint: f.try(:[], :hint) }
{ key: f.keys.first, title: f.values.first, hint: f.try(:[], :hint), type: f.fetch(:type, "text") }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"css-loader": "^5.2.6",
"css-minimizer-webpack-plugin": "^3.0.2",
"govuk-frontend": "^5.7.1",
"google-libphonenumber": "^3.2.38",
"mini-css-extract-plugin": "^2.0.0",
"node-sass": "^9.0.0",
"sass": "^1.35.1",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,11 @@ globule@^1.0.0:
lodash "^4.17.21"
minimatch "~3.0.2"

google-libphonenumber@^3.2.38:
version "3.2.38"
resolved "https://registry.yarnpkg.com/google-libphonenumber/-/google-libphonenumber-3.2.38.tgz#5adb0c8ff807d84d4d46b9536d0116d00d85f137"
integrity sha512-t/K0dsVmA0gMMVLJgcMeB9g1Ar4ANVWfkY+AJGSdfyJ2Ay7Bu8ceLYpUlC6FZSilZgaF1qbkM9tZydGBEBHqAg==

gopd@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.0.1.tgz#29ff76de69dac7489b7c0918a5788e56477c332c"
Expand Down

0 comments on commit 631018a

Please sign in to comment.