Improve validation and error handling on form field
This commit is contained in:
1 parent
0ea3379397
commit
4c2c452b91
9 files changed
+35
-13
No files matched your search
@@ -50,6 +50,7 @@ const Select = ({
|
||||
onChange,
|
||||
onBlur,
|
||||
sx,
|
||||
error = false,
|
||||
name = "",
|
||||
labelControlSpacing = 6,
|
||||
maxWidth,
|
||||
@@ -93,6 +94,7 @@ const Select = ({
|
||||
onChange={onChange}
|
||||
onBlur={onBlur}
|
||||
displayEmpty
|
||||
error={error}
|
||||
name={name}
|
||||
inputProps={{ id: id }}
|
||||
IconComponent={KeyboardArrowDownIcon}
|
||||
@@ -172,6 +174,7 @@ Select.propTypes = {
|
||||
label: PropTypes.string,
|
||||
placeholder: PropTypes.string,
|
||||
isHidden: PropTypes.bool,
|
||||
error: PropTypes.bool,
|
||||
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])
|
||||
.isRequired,
|
||||
items: PropTypes.arrayOf(
|
||||
|
||||
@@ -270,6 +270,11 @@ const UptimeCreate = ({ isClone = false }) => {
|
||||
|
||||
setMonitor((prev) => ({ ...prev, [name]: value }));
|
||||
|
||||
if (name === "type") {
|
||||
setErrors({});
|
||||
return;
|
||||
}
|
||||
|
||||
const { error } = monitorValidation.validate(
|
||||
{ type: monitor.type, [name]: value },
|
||||
{ abortEarly: false }
|
||||
@@ -277,7 +282,9 @@ const UptimeCreate = ({ isClone = false }) => {
|
||||
|
||||
setErrors((prev) => ({
|
||||
...prev,
|
||||
...(error ? { [name]: error.details[0].message } : { [name]: undefined }),
|
||||
...(error && error.details[0].path[0] === name
|
||||
? { [name]: error.details[0].message }
|
||||
: { [name]: undefined }),
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -593,6 +600,7 @@ const UptimeCreate = ({ isClone = false }) => {
|
||||
placeholder={t("chooseGame")}
|
||||
onChange={onChange}
|
||||
items={GAMELIST}
|
||||
error={errors["gameId"] ? true : false}
|
||||
/>
|
||||
)}
|
||||
<TextInput
|
||||
|
||||
@@ -184,12 +184,12 @@ const monitorValidation = joi.object({
|
||||
.min(1)
|
||||
.max(65535)
|
||||
.when("type", {
|
||||
is: "port",
|
||||
then: joi.number().messages({
|
||||
is: joi.valid("port", "game"),
|
||||
then: joi.required().messages({
|
||||
"number.base": "Port must be a number.",
|
||||
"number.min": "Port must be at least 1.",
|
||||
"number.max": "Port must be at most 65535.",
|
||||
"any.required": "Port is required for port monitors.",
|
||||
"any.required": "Port is required for port and game monitors.",
|
||||
}),
|
||||
otherwise: joi.optional(),
|
||||
}),
|
||||
@@ -205,7 +205,14 @@ const monitorValidation = joi.object({
|
||||
expectedValue: joi.string().allow(null, ""),
|
||||
jsonPath: joi.string().allow(null, ""),
|
||||
matchMethod: joi.string().allow(null, ""),
|
||||
gameId: joi.string().allow(null, ""),
|
||||
gameId: joi.when("type", {
|
||||
is: "game",
|
||||
then: joi.string().required().messages({
|
||||
"string.empty": "Game selection is required for game monitors.",
|
||||
"any.required": "Game selection is required for game monitors.",
|
||||
}),
|
||||
otherwise: joi.string().allow(null, ""),
|
||||
}),
|
||||
});
|
||||
|
||||
const imageValidation = joi.object({
|
||||
|
||||
Reference in new issue
Block a user