1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Validate new user username

Show a descriptive message when the username input doesn't match a modified version of the regex used by the backend
This commit is contained in:
SHestres 2024-12-18 08:15:43 +00:00
parent a88349f8d3
commit 918399e0ca
3 changed files with 23 additions and 3 deletions

View file

@ -205,6 +205,7 @@ const UserNew = () => {
type='text' type='text'
id='txtUsername' id='txtUsername'
label='LabelName' label='LabelName'
validator={{ pattern: '^([\\w \\-\'._@+]*)([\\w\\-\'._@+])([\\w \\-\'._@+]*)$', errMessage: 'Username must not be empty and contain only numbers, letters, spaces, or the following symbols -\'._@+' }}
options={'required'} options={'required'}
/> />
</div> </div>

View file

@ -2,23 +2,30 @@ import React, { type FC, useCallback, useEffect, useMemo, useRef } from 'react';
import globalize from 'lib/globalize'; import globalize from 'lib/globalize';
import './InputElementInvalidMessage.scss';
interface CreateInputElementParams { interface CreateInputElementParams {
type?: string type?: string
id?: string id?: string
label?: string label?: string
initialValue?: string initialValue?: string
validator?: { pattern: string, errMessage: string }
options?: string options?: string
} }
const createInputElement = ({ type, id, label, initialValue, options }: CreateInputElementParams) => ({ const createInputElement = ({ type, id, label, initialValue, validator, options }: CreateInputElementParams) => ({
__html: `<input __html: `<input
is="emby-input" is="emby-input"
type="${type}" type="${type}"
id="${id}" id="${id}"
label="${label}" label="${label}"
value="${initialValue}" value="${initialValue}"
${validator ? 'pattern="' + validator.pattern + '"' : ''}
${options} ${options}
/>` />
<div class="inputElementInvalidMessage">
${validator?.errMessage || ''}
</div>`
}); });
type InputElementProps = { type InputElementProps = {
@ -33,7 +40,9 @@ const InputElement: FC<InputElementProps> = ({
type, type,
id, id,
label, label,
validator,
options = '' options = ''
}) => { }) => {
const container = useRef<HTMLDivElement>(null); const container = useRef<HTMLDivElement>(null);
@ -44,14 +53,16 @@ const InputElement: FC<InputElementProps> = ({
id, id,
label: globalize.translate(label), label: globalize.translate(label),
initialValue, initialValue,
validator,
options options
}) })
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
), []); ), []);
const onInput = useCallback((e: Event) => { const onInput = useCallback((e: Event) => {
if (validator) (e.target as HTMLElement).parentElement?.querySelector('.inputElementInvalidMessage')?.classList.add('inputReadyForValidation');
onChange((e.target as HTMLInputElement).value); onChange((e.target as HTMLInputElement).value);
}, [ onChange ]); }, [ onChange, validator ]);
useEffect(() => { useEffect(() => {
const inputElement = container?.current?.querySelector<HTMLInputElement>('input'); const inputElement = container?.current?.querySelector<HTMLInputElement>('input');

View file

@ -0,0 +1,8 @@
.inputElementInvalidMessage {
color: red;
display: none;
}
.emby-input:invalid + .inputElementInvalidMessage.inputReadyForValidation {
display: block;
}