Skip to content

Commit b31e296

Browse files
authored
Merge pull request #701 from actiontech/fix/issue-503-2506.x
[fix](UserCenter): Add validate rule for user name
2 parents 01ce6a2 + 1f91c8e commit b31e296

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

packages/base/src/locale/zh-CN/dmsUserCenter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default {
1919
},
2020
userForm: {
2121
username: '用户名',
22+
usernameNoSpaces: '用户名不支持空格',
2223
needUpdatePassWord: '是否需要更新密码',
2324
passwordConfirm: '确认密码',
2425
passwordConfirmPlaceholder: '请保持两次密码输入一致',

packages/base/src/page/UserCenter/Drawer/User/UserForm/index.test.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { Form } from 'antd';
66
import { IUserFormFields, IUserFormProps } from './index.type';
77
import UserForm from '.';
8-
import { screen } from '@testing-library/react';
8+
import { screen, fireEvent, act } from '@testing-library/react';
99

1010
describe('base/UserCenter/Drawer/UserForm', () => {
1111
const customRender = (
@@ -36,4 +36,22 @@ describe('base/UserCenter/Drawer/UserForm', () => {
3636
expect(baseElement).toMatchSnapshot();
3737
expect(screen.queryByLabelText('是否禁用')).not.toBeInTheDocument();
3838
});
39+
40+
it('should show error when username contains spaces', async () => {
41+
const { result } = superRenderHook(() => Form.useForm<IUserFormFields>());
42+
const form = result.current[0];
43+
44+
superRender(<UserForm form={form} visible={true} />);
45+
46+
const usernameInput = screen.getByPlaceholderText('请输入用户名');
47+
48+
fireEvent.change(usernameInput, { target: { value: 'user name' } });
49+
50+
await act(async () => {
51+
const error = await form.validateFields(['username']).catch((err) => err);
52+
53+
expect(error.errorFields).toHaveLength(1);
54+
expect(error.errorFields[0].errors).toContain('用户名不支持空格');
55+
});
56+
});
3957
});

packages/base/src/page/UserCenter/Drawer/User/UserForm/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const UserForm: React.FC<IUserFormProps> = (props) => {
3535
message: t('common.form.rule.require', {
3636
name: t('dmsUserCenter.user.userForm.username')
3737
})
38+
},
39+
{
40+
pattern: /^\S*$/,
41+
message: t('dmsUserCenter.user.userForm.usernameNoSpaces')
3842
}
3943
]}
4044
>

0 commit comments

Comments
 (0)