-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathuseSignUpForm.js
More file actions
121 lines (109 loc) · 4.38 KB
/
useSignUpForm.js
File metadata and controls
121 lines (109 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
//
// Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).
//
// This program is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free Software
// Foundation; either version 3.0 of the License, or (at your option) any later
// version.
//
// Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with Greenlight; if not, see <http://www.gnu.org/licenses/>.
import * as yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { useTranslation } from 'react-i18next';
import { useForm } from 'react-hook-form';
import { useCallback, useMemo } from 'react';
export function useSignUpFormValidation(registrationMethod) {
const spec = {
name: yup.string().required('forms.validations.full_name.required')
.min(2, 'forms.validations.full_name.min')
.max(255, 'forms.validations.full_name.max'),
password: yup.string().max(255, 'forms.validations.password.max')
.matches(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[`@%~!#£$\\^&*()\][+={}/|:;"'<>\-,.?_ ]).{8,}$/,
'forms.validations.password.match',
)
.min(8, 'forms.validations.password.min')
.test('oneLower', 'forms.validations.password.lower', (pwd) => pwd.match(/[a-z]/))
.test('oneUpper', 'forms.validations.password.upper', (pwd) => pwd.match(/[A-Z]/))
.test('oneDigit', 'forms.validations.password.digit', (pwd) => pwd.match(/\d/))
.test('oneSymbol', 'forms.validations.password.symbol', (pwd) => pwd.match(/[`@%~!#£$\\^&*()\][+={}/|:;"'<>\-,.?_ ]/)),
password_confirmation: yup.string().required('forms.validations.password_confirmation.required')
.oneOf([yup.ref('password')], 'forms.validations.password_confirmation.match'),
}
if (registrationMethod !== 'invite') {
spec.email = yup.string().required('forms.validations.email.required').email('forms.validations.email.email')
.min(6, 'forms.validations.email.min')
.max(255, 'forms.validations.email.max')
}
return useMemo(() => (yup.object(spec)), []);
}
export default function useSignUpForm(registrationMethod, { defaultValues: _defaultValues, ..._config } = {}) {
const { t, i18n } = useTranslation();
const fields = useMemo(() => ({
name: {
label: t('forms.user.signup.fields.full_name.label'),
placeHolder: t('forms.user.signup.fields.full_name.placeholder'),
controlId: 'signupFormFullName',
hookForm: {
id: 'name',
},
},
email: {
label: t('forms.user.signup.fields.email.label'),
placeHolder: t('forms.user.signup.fields.email.placeholder'),
controlId: 'signupFormEmail',
hookForm: {
id: 'email',
},
},
password: {
label: t('forms.user.signup.fields.password.label'),
placeHolder: t('forms.user.signup.fields.password.placeholder'),
controlId: 'signupFormPwd',
hookForm: {
id: 'password',
validations: {
deps: ['password_confirmation'],
},
},
},
password_confirmation: {
label: t('forms.user.signup.fields.password_confirmation.label'),
placeHolder: t('forms.user.signup.fields.password_confirmation.placeholder'),
controlId: 'signupFormPwdConfirm',
hookForm: {
id: 'password_confirmation',
validations: {
deps: ['password'],
},
},
},
}), [i18n.resolvedLanguage]);
const validationSchema = useSignUpFormValidation(registrationMethod);
const config = useMemo(() => ({
...{
mode: 'onChange',
criteriaMode: 'all',
defaultValues: {
...{
name: '',
email: '',
password: '',
password_confirmation: '',
},
..._defaultValues,
},
resolver: yupResolver(validationSchema),
},
..._config,
}), [validationSchema, _defaultValues]);
const methods = useForm(config);
const reset = useCallback(() => methods.reset(config.defaultValues), [methods.reset, config.defaultValues]);
return { methods, fields, reset };
}