mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-20 10:00:58 +00:00
fix(inbounds): surface form validation errors (#6084)
* inbounds: surface form validation errors React Hook Form validation previously returned early without showing why an inbound save was blocked. Report the first field error and switch to the corresponding form tab so operators can correct it. * Fix inbound form tab error navigation Improve react-hook-form error traversal so validation stops on real `FieldError` leaves (detected by `type`) instead of any object with a `message`. This makes Save reliably jump to the tab containing the first invalid field and show the specific error, avoiding the previous generic/ambiguous invalid-state handling. --------- Co-authored-by: sonic <sonic@linux.do>
This commit is contained in:
@@ -127,6 +127,42 @@ function isValidShareAddrInput(value: string): boolean {
|
||||
return SHARE_ADDR_HOSTNAME_RE.test(v);
|
||||
}
|
||||
|
||||
interface RhfValidationIssue {
|
||||
path: PropertyKey[];
|
||||
message: string;
|
||||
}
|
||||
|
||||
function firstRhfValidationIssue(
|
||||
value: unknown,
|
||||
path: PropertyKey[] = [],
|
||||
): RhfValidationIssue | null {
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
const record = value as Record<string, unknown>;
|
||||
// `type` is what marks a react-hook-form leaf FieldError; anything else is a group.
|
||||
if ('type' in record) {
|
||||
return { path, message: typeof record.message === 'string' ? record.message : '' };
|
||||
}
|
||||
for (const key of Object.keys(record)) {
|
||||
const issue = firstRhfValidationIssue(record[key], [...path, key]);
|
||||
if (issue) return issue;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function tabForValidationPath(path: PropertyKey[]): string {
|
||||
if (path[0] === 'settings') return 'protocol';
|
||||
if (path[0] === 'sniffing') return 'sniffing';
|
||||
if (path[0] === 'streamSettings') {
|
||||
if (
|
||||
path[1] === 'security'
|
||||
|| path[1] === 'realitySettings'
|
||||
|| path[1] === 'tlsSettings'
|
||||
) return 'security';
|
||||
return 'stream';
|
||||
}
|
||||
return 'basic';
|
||||
}
|
||||
|
||||
interface InboundFormModalProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
@@ -195,6 +231,7 @@ export default function InboundFormModal({
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [scanning, setScanning] = useState(false);
|
||||
const [scanResult, setScanResult] = useState<RealityScanResult | null>(null);
|
||||
const [activeTab, setActiveTab] = useState('basic');
|
||||
const {
|
||||
fallbacks,
|
||||
fallbackChildOptions,
|
||||
@@ -356,6 +393,7 @@ export default function InboundFormModal({
|
||||
: buildAddModeValues();
|
||||
methods.reset(initial);
|
||||
setScanResult(null);
|
||||
setActiveTab('basic');
|
||||
const initialTag = (initial.tag ?? '') as string;
|
||||
autoTagRef.current = isAutoInboundTag(initialTag, {
|
||||
port: initial.port ?? 0,
|
||||
@@ -460,8 +498,7 @@ export default function InboundFormModal({
|
||||
/* eslint-disable-next-line react-hooks/exhaustive-deps */
|
||||
}, [mode, methods]);
|
||||
|
||||
const submit = async () => {
|
||||
if (!(await methods.trigger())) return;
|
||||
const saveValues = async () => {
|
||||
/*
|
||||
* getValues() returns the entire form store, including settings.clients and
|
||||
* settings.fallbacks which have no bound field (clients are managed via the
|
||||
@@ -503,6 +540,17 @@ export default function InboundFormModal({
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Field errors render inline, but every tab is force-rendered, so an error on
|
||||
* a hidden tab looks like a dead Save button — jump to it and say what broke.
|
||||
*/
|
||||
const submit = methods.handleSubmit(saveValues, (errors) => {
|
||||
const issue = firstRhfValidationIssue(errors);
|
||||
if (!issue) return;
|
||||
setActiveTab(tabForValidationPath(issue.path));
|
||||
messageApi.error(formatInboundIssue(issue, methods.getValues(), t));
|
||||
});
|
||||
|
||||
const title = mode === 'edit'
|
||||
? t('pages.inbounds.modifyInbound')
|
||||
: t('pages.inbounds.addInbound');
|
||||
@@ -961,7 +1009,7 @@ export default function InboundFormModal({
|
||||
wrapperCol={{ sm: { span: 14 } }}
|
||||
labelWrap
|
||||
>
|
||||
<Tabs items={[
|
||||
<Tabs activeKey={activeTab} onChange={setActiveTab} items={[
|
||||
{ key: 'basic', label: t('pages.xray.basicTemplate'), children: basicTab, forceRender: true },
|
||||
...(([
|
||||
Protocols.VLESS,
|
||||
|
||||
Reference in New Issue
Block a user