Merge pull request #46 from R15-Software-Dev/feat/email-options
Create new email validation system
This commit was merged in pull request #46.
This commit is contained in:
4 files changed
+26
-15
No files matched your search
@@ -13,9 +13,15 @@ pub enum TextFieldType {
|
|||||||
Text,
|
Text,
|
||||||
/// A text field that accepts only numbers.
|
/// A text field that accepts only numbers.
|
||||||
Number,
|
Number,
|
||||||
/// A text field that accepts only Region 15 email addresses.
|
/// A text field that accepts the specified email domains.
|
||||||
/// TODO Accept more than just Region 15 addresses.
|
/// Values should be given as the string following the '@' in an address, or '*' for any.
|
||||||
Email
|
///
|
||||||
|
/// Example usage:
|
||||||
|
/// ```
|
||||||
|
/// TextFieldType::Email(vec!["gmail.com".to_string(), "customdomain.org".to_string()]);
|
||||||
|
/// TextFieldType::Email(vec!["*".to_string()]);
|
||||||
|
/// ```
|
||||||
|
Email(Vec<String>)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Entry point for validating a text field.
|
/// Entry point for validating a text field.
|
||||||
@@ -24,7 +30,7 @@ fn validate(required: bool, value: &str, input_type: &TextFieldType) -> Validati
|
|||||||
ValidationState::Invalid("This field is required.".to_string())
|
ValidationState::Invalid("This field is required.".to_string())
|
||||||
} else {
|
} else {
|
||||||
match input_type {
|
match input_type {
|
||||||
TextFieldType::Email => validate_email(value),
|
TextFieldType::Email(domains) => validate_email(value, domains),
|
||||||
TextFieldType::Number => validate_number(value),
|
TextFieldType::Number => validate_number(value),
|
||||||
TextFieldType::Text => ValidationState::Valid
|
TextFieldType::Text => ValidationState::Valid
|
||||||
}
|
}
|
||||||
@@ -32,15 +38,18 @@ fn validate(required: bool, value: &str, input_type: &TextFieldType) -> Validati
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Validates an email address.
|
/// Validates an email address.
|
||||||
fn validate_email(input: &str) -> ValidationState {
|
fn validate_email(input: &str, valid_domains: &Vec<String>) -> ValidationState {
|
||||||
// TODO Accept more than just Region 15 addresses.
|
// Check for wildcard domain. We won't check domains if it's present.
|
||||||
let pattern = regex::Regex::new(r"^[a-zA-Z0-9._%+-]+@region15\.org$").expect("Invalid regex pattern");
|
let wild = valid_domains.contains(&"*".to_string());
|
||||||
|
|
||||||
if pattern.is_match(input) {
|
input.split_once('@')
|
||||||
ValidationState::Valid
|
.and_then(|(_, domain)| {
|
||||||
} else {
|
(wild || valid_domains.contains(&domain.to_string()))
|
||||||
ValidationState::Invalid("Please provide a valid Region 15 email address.".to_string())
|
.then(|| ValidationState::Valid)
|
||||||
}
|
.or_else(|| Some(ValidationState::Invalid(
|
||||||
|
format!("Email address must match one of the following: {}", valid_domains.join(", "))
|
||||||
|
)))
|
||||||
|
}).unwrap_or(ValidationState::Invalid("Invalid email address.".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validates a number.
|
/// Validates a number.
|
||||||
|
|||||||
@@ -285,7 +285,9 @@ pub fn HomePage() -> impl IntoView {
|
|||||||
disabled=elements_disabled
|
disabled=elements_disabled
|
||||||
data_member="contact_email"
|
data_member="contact_email"
|
||||||
data_map=form_data
|
data_map=form_data
|
||||||
input_type=TextFieldType::Email
|
input_type=TextFieldType::Email(
|
||||||
|
vec!["fakeemail.org".to_string(), "gmail.com".to_string()]
|
||||||
|
)
|
||||||
required=true
|
required=true
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ pub fn LoanerBorrowForm() -> impl IntoView {
|
|||||||
disabled=elements_disabled
|
disabled=elements_disabled
|
||||||
data_member="email"
|
data_member="email"
|
||||||
data_map=data
|
data_map=data
|
||||||
input_type=TextFieldType::Email
|
input_type=TextFieldType::Email(vec!["region15.org".to_string()])
|
||||||
required=true
|
required=true
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ pub fn ProviderContactPage() -> impl IntoView {
|
|||||||
disabled=elements_disabled
|
disabled=elements_disabled
|
||||||
data_member="contact_email"
|
data_member="contact_email"
|
||||||
data_map=contact_info
|
data_map=contact_info
|
||||||
input_type=TextFieldType::Email
|
input_type=TextFieldType::Email(vec!["*".to_string()])
|
||||||
required=true
|
required=true
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
|
|||||||
Reference in new issue
Block a user