Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

improve edit account form

x032205c91c5373be9c
diff --git a/frontend/src/hooks/api/pam/mutations.tsx b/frontend/src/hooks/api/pam/mutations.tsx
index f0db46ba3a..dda9bad535 100644
--- a/frontend/src/hooks/api/pam/mutations.tsx
+++ b/frontend/src/hooks/api/pam/mutations.tsx
@@ -102,6 +102,8 @@ export const useTerminatePamSession = () => {
export const useCreatePamAccount = () => {
const queryClient = useQueryClient();
return useMutation({
+ // Validation errors are mapped onto the form fields
+ meta: { skipValidationToast: true },
mutationFn: async ({ accountType, ...params }: TCreatePamAccountDTO) => {
const { data } = await apiRequest.post(`/api/v1/pam/accounts/${accountType}`, params);
return data.account;
@@ -116,6 +118,8 @@ export const useCreatePamAccount = () => {
export const useUpdatePamAccount = () => {
const queryClient = useQueryClient();
return useMutation({
+ // Validation errors are mapped onto the form fields
+ meta: { skipValidationToast: true },
mutationFn: async ({ accountId, accountType, ...params }: TUpdatePamAccountDTO) => {
const { data } = await apiRequest.patch(
`/api/v1/pam/accounts/${accountType}/${accountId}`,
diff --git a/frontend/src/hooks/api/reactQuery.tsx b/frontend/src/hooks/api/reactQuery.tsx
index b454b870a0..96aa61405e 100644
--- a/frontend/src/hooks/api/reactQuery.tsx
+++ b/frontend/src/hooks/api/reactQuery.tsx
@@ -87,10 +87,18 @@ function ValidationErrorModal({
);
}
-export const onRequestError = (error: unknown) => {
+export const onRequestError = (
+ error: unknown,
+ _variables?: unknown,
+ _context?: unknown,
+ mutation?: { meta?: Record<string, unknown> }
+) => {
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as TApiErrors;
if (serverResponse?.error === ApiErrorTypes.ValidationError) {
+ if (mutation?.meta?.skipValidationToast) {
+ return;
+ }
let requestBody: Record<string, unknown> | undefined;
try {
const configData = error.config?.data;
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/CreateAccountSheet.tsx b/frontend/src/pages/pam/PamAccountsPage/components/CreateAccountSheet.tsx
index dd148eb2d9..1cf0f3d340 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/CreateAccountSheet.tsx
+++ b/frontend/src/pages/pam/PamAccountsPage/components/CreateAccountSheet.tsx
@@ -51,8 +51,9 @@ import {
import { AccountPlatformIcon } from "../../PamAccessPage/components/AccountPlatformIcon";
import {
accountFormSchema,
- areRequiredFieldsFilled,
+ applyServerValidationErrors,
buildDefaultFieldValues,
+ getMissingRequiredFields,
TAccountFormValues
} from "./accountFormSchema";
import { ConnectionDetailsForm } from "./ConnectionDetailsForm";
@@ -77,6 +78,8 @@ export const CreateAccountSheet = ({ isOpen, onOpenChange, defaultFolderId }: Pr
reset,
watch,
setValue,
+ setError,
+ clearErrors,
formState: { isDirty }
} = useForm<TAccountFormValues>({
resolver: zodResolver(accountFormSchema),
@@ -141,12 +144,25 @@ export const CreateAccountSheet = ({ isOpen, onOpenChange, defaultFolderId }: Pr
const onSubmit = (values: TAccountFormValues) => {
if (!selectedMetadata) return;
- if (!areRequiredFieldsFilled(selectedMetadata.connectionFields, values.connectionDetails)) {
- createNotification({ text: "Check connection details fields", type: "error" });
- return;
- }
- if (!areRequiredFieldsFilled(selectedMetadata.credentialFields, values.credentials)) {
- createNotification({ text: "Check credentials fields", type: "error" });
+ clearErrors();
+ const missingConnection = getMissingRequiredFields(
+ selectedMetadata.connectionFields,
+ values.connectionDetails
+ );
+ const missingCredentials = getMissingRequiredFields(
+ selectedMetadata.credentialFields,
+ values.credentials
+ );
+ if (missingConnection.length || missingCredentials.length) {
+ missingConnection.forEach((key) =>
+ setError(`connectionDetails.${key}`, {
+ type: "required",
+ message: "This field is required"
+ })
+ );
+ missingCredentials.forEach((key) =>
+ setError(`credentials.${key}`, { type: "required", message: "This field is required" })
+ );
return;
}
@@ -164,7 +180,8 @@ export const CreateAccountSheet = ({ isOpen, onOpenChange, defaultFolderId }: Pr
onSuccess: () => {
createNotification({ text: "Account created", type: "success" });
onOpenChange(false);
- }
+ },
+ onError: (error) => applyServerValidationErrors(error, setError)
}
);
};
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx b/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
index df40d9a826..8917f3da58 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
+++ b/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
@@ -27,9 +27,10 @@ import { UNCHANGED_PASSWORD_SENTINEL } from "@app/hooks/api/pam/constants";
import { SheetSaveBar } from "../../components/SheetSaveBar";
import {
accountFormSchema,
- areRequiredFieldsFilled,
+ applyServerValidationErrors,
buildDefaultFieldValues,
buildEditCredentialValues,
+ getMissingRequiredFields,
TAccountFormValues
} from "./accountFormSchema";
import { ConnectionDetailsForm } from "./ConnectionDetailsForm";
@@ -86,6 +87,8 @@ export const EditAccountForm = ({ accountId, onDirtyChange }: Props) => {
control,
handleSubmit,
reset,
+ setError,
+ clearErrors,
formState: { isDirty }
} = useForm<TAccountFormValues>({
resolver: zodResolver(accountFormSchema)
@@ -116,12 +119,25 @@ export const EditAccountForm = ({ accountId, onDirtyChange }: Props) => {
const onSubmit = (values: TAccountFormValues) => {
if (!accountId || !account || !metadata) return;
- if (!areRequiredFieldsFilled(metadata.connectionFields, values.connectionDetails)) {
- createNotification({ text: "Check connection details fields", type: "error" });
- return;
- }
- if (!areRequiredFieldsFilled(metadata.credentialFields, values.credentials)) {
- createNotification({ text: "Check credentials fields", type: "error" });
+ clearErrors();
+ const missingConnection = getMissingRequiredFields(
+ metadata.connectionFields,
+ values.connectionDetails
+ );
+ const missingCredentials = getMissingRequiredFields(
+ metadata.credentialFields,
+ values.credentials
+ );
+ if (missingConnection.length || missingCredentials.length) {
+ missingConnection.forEach((key) =>
+ setError(`connectionDetails.${key}`, {
+ type: "required",
+ message: "This field is required"
+ })
+ );
+ missingCredentials.forEach((key) =>
+ setError(`credentials.${key}`, { type: "required", message: "This field is required" })
+ );
return;
}
@@ -140,7 +156,8 @@ export const EditAccountForm = ({ accountId, onDirtyChange }: Props) => {
...(filteredCredentials ? { credentials: filteredCredentials } : {})
},
{
- onSuccess: () => createNotification({ text: "Account updated", type: "success" })
+ onSuccess: () => createNotification({ text: "Account updated", type: "success" }),
+ onError: (error) => applyServerValidationErrors(error, setError)
}
);
};
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/PamSchemaFields.tsx b/frontend/src/pages/pam/PamAccountsPage/components/PamSchemaFields.tsx
index c4979f2d47..12f8388675 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/PamSchemaFields.tsx
+++ b/frontend/src/pages/pam/PamAccountsPage/components/PamSchemaFields.tsx
@@ -107,7 +107,7 @@ export const PamSchemaFields = ({ control, namePrefix, fields }: Props) => {
<Field orientation="horizontal">
<FieldLabel>
{descriptor.label}
- {descriptor.required && <RequiredMark />}
+ {(descriptor.required || descriptor.secret) && <RequiredMark />}
</FieldLabel>
<Switch
variant="pam"
@@ -119,7 +119,7 @@ export const PamSchemaFields = ({ control, namePrefix, fields }: Props) => {
<Field>
<FieldLabel>
{descriptor.label}
- {descriptor.required && <RequiredMark />}
+ {(descriptor.required || descriptor.secret) && <RequiredMark />}
</FieldLabel>
<FieldContent>
<FieldWidget field={field} descriptor={descriptor} isError={!!fieldState.error} />
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
index 3be941539a..34fd723e31 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
+++ b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
@@ -1,7 +1,10 @@
+import { FieldValues, Path, UseFormSetError } from "react-hook-form";
+import axios from "axios";
import { z } from "zod";
import { PamAccountType, PamFieldWidget, TPamFieldDescriptor } from "@app/hooks/api/pam";
import { UNCHANGED_PASSWORD_SENTINEL } from "@app/hooks/api/pam/constants";
+import { ApiErrorTypes, TApiErrors } from "@app/hooks/api/types";
// Shared shape only; per-type connection/credential fields are validated server-side
export const accountFormSchema = z.object({
@@ -39,18 +42,33 @@ export const buildEditCredentialValues = (
const isFieldVisible = (field: TPamFieldDescriptor, values: Record<string, unknown>) =>
!field.showWhen || values[field.showWhen.field] === field.showWhen.equals;
-// Lightweight required-field check; the backend performs full schema validation
-export const areRequiredFieldsFilled = (
+export const getMissingRequiredFields = (
fields: TPamFieldDescriptor[],
values: Record<string, unknown>
-): boolean =>
- fields.every((field) => {
- if (
- !field.required ||
- field.widget === PamFieldWidget.Boolean ||
- !isFieldVisible(field, values)
- )
- return true;
- const value = values[field.key];
- return value !== undefined && value !== null && String(value).trim() !== "";
+): string[] =>
+ fields
+ .filter((field) => {
+ if (field.widget === PamFieldWidget.Boolean || !isFieldVisible(field, values)) return false;
+ if (!field.required && !field.secret) return false;
+ const value = values[field.key];
+ return value === undefined || value === null || String(value).trim() === "";
+ })
+ .map((field) => field.key);
+
+// Maps backend validation issues onto form fields
+export const applyServerValidationErrors = <T extends FieldValues>(
+ error: unknown,
+ setError: UseFormSetError<T>
+): boolean => {
+ if (!axios.isAxiosError(error)) return false;
+ const data = error.response?.data as TApiErrors | undefined;
+ if (data?.error !== ApiErrorTypes.ValidationError) return false;
+
+ let mapped = false;
+ data.message.forEach((issue) => {
+ if (!issue.path.length) return;
+ setError(issue.path.join(".") as Path<T>, { type: "server", message: issue.message });
+ mapped = true;
});
+ return mapped;
+};