From b52093d1885a24fa19f9583ea2d282cc4575f987 Mon Sep 17 00:00:00 2001 From: Simon Kobyda Date: Thu, 13 Jul 2023 12:06:58 +0200 Subject: [PATCH] Validate Environment variables fields in Create container dialog --- src/Env.jsx | 54 ++++++++++++++++++++++++++++++++++++++---- src/ImageRunModal.jsx | 20 ++++++++++++---- test/check-application | 9 +++++++ 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/Env.jsx b/src/Env.jsx index 1944a963a..5cd903781 100644 --- a/src/Env.jsx +++ b/src/Env.jsx @@ -1,13 +1,35 @@ import React from 'react'; import { Button } from "@patternfly/react-core/dist/esm/components/Button"; import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form"; +import { FormHelper } from "cockpit-components-form-helper.jsx"; import { Grid } from "@patternfly/react-core/dist/esm/layouts/Grid"; import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput"; import { TrashIcon } from '@patternfly/react-icons'; import cockpit from 'cockpit'; +import * as utils from './util.js'; + const _ = cockpit.gettext; +export function validateEnvVar(env, key, validationFailed) { + const delta = { ...validationFailed }; + delete delta[key]; + + switch (key) { + case "envKey": + if (utils.isEmpty(env)) + delta.envKey = _("Key must not be empty"); + break; + case "envValue": + if (utils.isEmpty(env)) + delta.envValue = _("Value must not be empty"); + + break; + } + + return delta; +} + const handleEnvValue = (key, value, idx, onChange, additem, itemCount, companionField) => { // Allow the input of KEY=VALUE separated value pairs for bulk import only if the other // field is not empty. @@ -32,18 +54,40 @@ const handleEnvValue = (key, value, idx, onChange, additem, itemCount, companion } }; -export const EnvVar = ({ id, item, onChange, idx, removeitem, additem, itemCount }) => +export const EnvVar = ({ id, item, onChange, idx, removeitem, additem, itemCount, validationFailed, onValidationChange }) => ( - + handleEnvValue('envKey', value, idx, onChange, additem, itemCount, item.envValue)} /> + validated={validationFailed?.envKey ? "error" : "default"} + onChange={(_event, value) => { + utils.validationClear(validationFailed, "envKey", onValidationChange); + utils.validationDebounce(() => onValidationChange(validateEnvVar(value, "envKey", validationFailed))); + handleEnvValue('envKey', value, idx, onChange, additem, itemCount, item.envValue); + }} /> + - + handleEnvValue('envValue', value, idx, onChange, additem, itemCount, item.envKey)} /> + validated={validationFailed?.envValue ? "error" : "default"} + onChange={(_event, value) => { + utils.validationClear(validationFailed, "envValue", onValidationChange); + utils.validationDebounce(() => onValidationChange(validateEnvVar(value, "envValue", validationFailed))); + handleEnvValue('envValue', value, idx, onChange, additem, itemCount, item.envValue); + }} /> +