Validate the input from the user (#2242)

This commit is contained in:
Erik Jan de Wit 2022-03-14 11:26:36 +01:00 committed by GitHub
parent b426886e6a
commit a3105e9327
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View file

@ -24,7 +24,12 @@ import { toClients } from "../routes/Clients";
export default function CreateInitialAccessToken() { export default function CreateInitialAccessToken() {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const { handleSubmit, control } = useForm(); const {
handleSubmit,
control,
errors,
formState: { isValid },
} = useForm({ mode: "onChange" });
const adminClient = useAdminClient(); const adminClient = useAdminClient();
const { realm } = useRealm(); const { realm } = useRealm();
@ -76,16 +81,21 @@ export default function CreateInitialAccessToken() {
fieldLabelId="clients:expiration" fieldLabelId="clients:expiration"
/> />
} }
helperTextInvalid={t("expirationValueNotValid")}
validated={errors.expiration ? "error" : "default"}
> >
<Controller <Controller
name="expiration" name="expiration"
defaultValue={86400} defaultValue={86400}
control={control} control={control}
rules={{ min: 1 }}
render={({ onChange, value }) => ( render={({ onChange, value }) => (
<TimeSelector <TimeSelector
data-testid="expiration" data-testid="expiration"
value={value} value={value}
onChange={onChange} onChange={onChange}
min={1}
validated={errors.expiration ? "error" : "default"}
/> />
)} )}
/> />
@ -113,15 +123,23 @@ export default function CreateInitialAccessToken() {
value={value} value={value}
onPlus={() => onChange(value + 1)} onPlus={() => onChange(value + 1)}
onMinus={() => onChange(value - 1)} onMinus={() => onChange(value - 1)}
onChange={(event) => onChange={(event) => {
onChange(Number((event.target as HTMLInputElement).value)) const value = Number(
} (event.target as HTMLInputElement).value
);
onChange(value < 1 ? 1 : value);
}}
/> />
)} )}
/> />
</FormGroup> </FormGroup>
<ActionGroup> <ActionGroup>
<Button variant="primary" type="submit" data-testid="save"> <Button
variant="primary"
type="submit"
data-testid="save"
isDisabled={!isValid}
>
{t("common:save")} {t("common:save")}
</Button> </Button>
<Button <Button

View file

@ -293,6 +293,7 @@ export default {
clientList: "Clients", clientList: "Clients",
clientsList: "Clients list", clientsList: "Clients list",
initialAccessToken: "Initial access token", initialAccessToken: "Initial access token",
expirationValueNotValid: "Value should should be greater or equal to 1",
clientSettings: "Client details", clientSettings: "Client details",
selectEncryptionType: "Select Encryption type", selectEncryptionType: "Select Encryption type",
generalSettings: "General Settings", generalSettings: "General Settings",