Skip to content

Commit 9e3c6b2

Browse files
authored
Make validation for username field work (#2945)
1 parent 0ffade2 commit 9e3c6b2

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

frontend/public/src/components/forms/new_ProfileFormFields.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "../../constants"
1515
import FormError from "./elements/FormError"
1616
import CardLabel from "../input/CardLabel"
17+
import { usernameFieldErrorMessage } from "../../lib/validation"
1718

1819
export const NAME_REGEX =
1920
/^(?![~!@&)(+:'.?,-])(?!.*[(/^$#*=[\]`%_;\\<>{}"|)]).*$/
@@ -25,7 +26,17 @@ export const NAME_REGEX_FAIL_MESSAGE =
2526
"Name cannot start with a special character (~!@&)(+:'.?,-), and cannot contain any of (/^$#*=[]`%_;\\<>{}\"|)"
2627

2728
export const legalAddressValidation = yup.object().shape({
28-
name: yup.string().required().label("Full Name").min(2).max(254),
29+
name: yup.string().required().label("Full Name").min(2).max(254),
30+
username: yup
31+
.string()
32+
.required()
33+
.label("Public Username")
34+
.min(3, usernameFieldErrorMessage)
35+
.max(30, usernameFieldErrorMessage)
36+
.matches(
37+
/^[^!@#$%^&*()+=\[\]{};':"\\|,<>\/?~]*$/, // eslint-disable-line no-useless-escape
38+
"Username cannot contain any of these !@#$%^&*()+=[]{};':\"\\|,<>/?~ symbols"
39+
),
2940
legal_address: yup.object().shape({
3041
first_name: yup
3142
.string()
@@ -231,30 +242,27 @@ export const LegalAddressFields = ({
231242
/>
232243
<ErrorMessage name="name" component={FormError} />
233244
</div>
234-
{isNewAccount ? (
235-
<React.Fragment>
236-
<div className="form-group">
237-
<CardLabel
238-
htmlFor="username"
239-
isRequired={true}
240-
label="Public Username"
241-
subLabel="Name that will identify you in courses"
242-
/>
243-
<Field
244-
type="text"
245-
name="username"
246-
className="form-control"
247-
autoComplete="username"
248-
id="username"
249-
aria-invalid={errors.username ? "true" : null}
250-
aria-describedby={errors.username ? "username-error" : null}
251-
aria-description="Name that will identify you in courses."
252-
required
253-
/>
254-
<ErrorMessage name="username" component={FormError} />
255-
</div>
256-
</React.Fragment>
257-
) : null}
245+
246+
<div className="form-group">
247+
<CardLabel
248+
htmlFor="username"
249+
isRequired={true}
250+
label="Public Username"
251+
subLabel="Name that will identify you in courses"
252+
/>
253+
<Field
254+
type="text"
255+
name="username"
256+
className="form-control"
257+
autoComplete="username"
258+
id="username"
259+
aria-invalid={errors.username ? "true" : null}
260+
aria-describedby={errors.username ? "username-error" : null}
261+
aria-description="Name that will identify you in courses."
262+
required
263+
/>
264+
<ErrorMessage name="username" component={FormError} />
265+
</div>
258266
<div className="form-group">
259267
<CardLabel
260268
htmlFor="legal_address.country"

0 commit comments

Comments
 (0)