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

Check name contains at least one letter #3465

Merged
merged 1 commit into from
Dec 19, 2024
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
3 changes: 2 additions & 1 deletion app/models/name_format_validator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class NameFormatValidator < ActiveModel::EachValidator
NAME_REGEX_FILTER = /\A[^\[\]\^"=$%#&*+\/\\()@?!<>_`|{}~0-9]*\z/
HAS_LETTERS_FILTER = /[a-zA-Z]/

def validate_each(record, attribute, value)
return unless value.present?

unless NAME_REGEX_FILTER.match?(value)
unless NAME_REGEX_FILTER.match?(value) && HAS_LETTERS_FILTER.match?(value)
record.errors.add(attribute, options[:message])
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/forms/personal_details_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
it { should validate_length_of(:first_name).is_at_most(100).with_message("First name must be less than 100 characters") }
it { should_not allow_value("*").for(:first_name).with_message("First name cannot contain special characters") }
it { should allow_value("O'Brian").for(:first_name) }
it { should_not allow_value(".").for(:first_name) }

it { should validate_length_of(:middle_name).is_at_most(61).with_message("Middle names must be less than 61 characters") }
it { should_not allow_value("&").for(:middle_name).with_message("Middle names cannot contain special characters") }
Expand All @@ -280,6 +281,7 @@
it { should validate_length_of(:surname).is_at_most(100).with_message("Last name must be less than 100 characters") }
it { should_not allow_value("$").for(:surname).with_message("Last name cannot contain special characters") }
it { should allow_value("O'Brian").for(:surname) }
it { should_not allow_value(".").for(:surname) }

it { should validate_presence_of(:national_insurance_number).with_message("Enter a National Insurance number in the correct format") }
it { should allow_value("QQ123456C").for(:national_insurance_number) }
Expand Down
Loading