Skip to content

Commit

Permalink
Fix and organize email/username validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed May 23, 2024
1 parent d6564f0 commit f2d011a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 8 additions & 2 deletions frontend/src/lib/components/Users/CreateUser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
email: string;
};
let turnstileToken = '';
function validateAsEmail(value: string): boolean {
return !allowUsernames || value.includes('@');
}
// $locale is the locale that our i18n is using for them (i.e. the best available option we have for them)
// getLanguageCodeFromNavigator() gives us the language/locale they probably actually want. Maybe we'll support it in the future.
const userLocale = getLanguageCodeFromNavigator() ?? $locale;
const formSchema = z.object({
name: z.string().trim().min(1, $t('register.name_missing')),
email: z.string().trim()
.min(1, $t('project_page.add_user.empty_user_field'))
.refine((value) => isEmail(value) || (allowUsernames && usernameRe.test(value)), (value) => ({ message: isEmail(value) ? $t('register.invalid_email', { email: value }) : $t('register.invalid_username', {username: value}) })),
.refine((value) => !validateAsEmail(value) || isEmail(value), $t('form.invalid_email'))
.refine((value) => validateAsEmail(value) || usernameRe.test(value), $t('register.invalid_username')),
password: passwordFormRules($t),
score: z.number(),
locale: z.string().trim().min(2).default(userLocale),
Expand All @@ -43,7 +49,7 @@
$errors.email = [$t('register.account_exists')];
}
if (error.invalidInput) {
$errors.email = [$t('register.invalid_input', { username: $form.email })];
$errors.email = [validateAsEmail($form.email) ? $t('form.invalid_email') : $t('register.invalid_username')];
}
return;
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/lib/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ If you don't see a dialog or already closed it, click the button below:",
"register": {
"title": "Register",
"account_exists": "An account with this email already exists",
"invalid_email": "Invalid email address: {email}",
"invalid_username": "Invalid login/username: {username}. Only letters, numbers, and underscore (_) characters are allowed.",
"invalid_input": "Invalid email address or login/username: {username}. Only letters, numbers, and underscore (_) characters are allowed.",
"invalid_username": "Invalid login/username. Only letters, numbers, and underscore (_) characters are allowed.",
"button_register": "Register",
"label_email": "Email",
"label_email_or_username": "Email or login/username",
Expand Down

0 comments on commit f2d011a

Please sign in to comment.