make resouce field required (#2290)

fixes: #2061
This commit is contained in:
Erik Jan de Wit 2022-03-22 12:13:43 +01:00 committed by GitHub
parent b400b2f312
commit 8631584920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View file

@ -196,6 +196,8 @@ export default function PermissionDetails() {
<FormGroup <FormGroup
label={t("common:name")} label={t("common:name")}
isRequired isRequired
helperTextInvalid={t("common:required")}
validated={errors.name ? "error" : "default"}
fieldId="name" fieldId="name"
labelIcon={ labelIcon={
<HelpItem <HelpItem
@ -208,6 +210,7 @@ export default function PermissionDetails() {
id="name" id="name"
name="name" name="name"
ref={register({ required: true })} ref={register({ required: true })}
validated={errors.name ? "error" : "default"}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -282,6 +285,9 @@ export default function PermissionDetails() {
fieldLabelId="clients:resources" fieldLabelId="clients:resources"
/> />
} }
helperTextInvalid={t("common:required")}
validated={errors.resources ? "error" : "default"}
isRequired
> >
<ResourcesPolicySelect <ResourcesPolicySelect
name="resources" name="resources"
@ -295,6 +301,7 @@ export default function PermissionDetails() {
? SelectVariant.typeahead ? SelectVariant.typeahead
: SelectVariant.typeaheadMulti : SelectVariant.typeaheadMulti
} }
isRequired
/> />
</FormGroup> </FormGroup>
)} )}

View file

@ -8,11 +8,12 @@ import type { Clients } from "@keycloak/keycloak-admin-client/lib/resources/clie
import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
type ResourcesPolicySelectProps = { type ResourcesPolicySelectProps = {
name: string; name: keyof PolicyRepresentation;
clientId: string; clientId: string;
searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">; searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">;
variant?: SelectVariant; variant?: SelectVariant;
preSelected?: string; preSelected?: string;
isRequired?: boolean;
}; };
type Policies = { type Policies = {
@ -26,11 +27,12 @@ export const ResourcesPolicySelect = ({
clientId, clientId,
variant = SelectVariant.typeaheadMulti, variant = SelectVariant.typeaheadMulti,
preSelected, preSelected,
isRequired = false,
}: ResourcesPolicySelectProps) => { }: ResourcesPolicySelectProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const adminClient = useAdminClient(); const adminClient = useAdminClient();
const { control } = useFormContext<PolicyRepresentation>(); const { control, errors } = useFormContext<PolicyRepresentation>();
const [items, setItems] = useState<Policies[]>([]); const [items, setItems] = useState<Policies[]>([]);
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
@ -64,6 +66,7 @@ export const ResourcesPolicySelect = ({
name={name} name={name}
defaultValue={preSelected ? [preSelected] : []} defaultValue={preSelected ? [preSelected] : []}
control={control} control={control}
rules={{ validate: (value) => !isRequired || value.length > 0 }}
render={({ onChange, value }) => ( render={({ onChange, value }) => (
<Select <Select
toggleId={name} toggleId={name}
@ -89,6 +92,7 @@ export const ResourcesPolicySelect = ({
isOpen={open} isOpen={open}
aria-labelledby={t(name)} aria-labelledby={t(name)}
isDisabled={!!preSelected} isDisabled={!!preSelected}
validated={errors[name] ? "error" : "default"}
> >
{toSelectOptions()} {toSelectOptions()}
</Select> </Select>