mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-06-08 21:38:21 +00:00
Highlighting inputs with errors
This commit is contained in:
parent
13b45e62b8
commit
c17d80514a
@ -439,24 +439,13 @@ public class DefaultDbAdminController {
|
||||
pkValue = null;
|
||||
}
|
||||
|
||||
if (pkValue == null) {
|
||||
try {
|
||||
if (pkValue == null) {
|
||||
Object newPrimaryKey = repository.create(schema, params, files, pkValue);
|
||||
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
|
||||
pkValue = newPrimaryKey.toString();
|
||||
attr.addFlashAttribute("message", "Item created successfully.");
|
||||
saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE", schema.getClassName()));
|
||||
} catch (DataIntegrityViolationException | UncategorizedSQLException | IdentifierGenerationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to INSERT row");
|
||||
attr.addFlashAttribute("error", e.getMessage());
|
||||
attr.addFlashAttribute("params", params);
|
||||
} catch (ConstraintViolationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (validation error)");
|
||||
attr.addFlashAttribute("error", "See below for details");
|
||||
attr.addFlashAttribute("validationErrors", new ValidationErrorsContainer(e));
|
||||
attr.addFlashAttribute("params", params);
|
||||
}
|
||||
|
||||
} else {
|
||||
Optional<DbObject> object = repository.findById(schema, pkValue);
|
||||
|
||||
@ -466,40 +455,29 @@ public class DefaultDbAdminController {
|
||||
attr.addFlashAttribute("error", "Item with id " + object.get().getPrimaryKeyValue() + " already exists.");
|
||||
attr.addFlashAttribute("params", params);
|
||||
} else {
|
||||
try {
|
||||
repository.update(schema, params, files);
|
||||
repository.attachManyToMany(schema, pkValue, multiValuedParams);
|
||||
attr.addFlashAttribute("message", "Item saved successfully.");
|
||||
saveAction(new UserAction(schema.getTableName(), pkValue, "EDIT", schema.getClassName()));
|
||||
} catch (DataIntegrityViolationException | UncategorizedSQLException | IdentifierGenerationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to UPDATE row (no changes applied)");
|
||||
attr.addFlashAttribute("error", e.getMessage());
|
||||
attr.addFlashAttribute("params", params);
|
||||
} catch (ConstraintViolationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (validation error)");
|
||||
attr.addFlashAttribute("error", "See below for details");
|
||||
attr.addFlashAttribute("validationErrors", new ValidationErrorsContainer(e));
|
||||
attr.addFlashAttribute("params", params);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Object newPrimaryKey = repository.create(schema, params, files, pkValue);
|
||||
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
|
||||
attr.addFlashAttribute("message", "Item created successfully");
|
||||
saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE", schema.getClassName()));
|
||||
}
|
||||
}
|
||||
} catch (DataIntegrityViolationException | UncategorizedSQLException | IdentifierGenerationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (no changes applied)");
|
||||
attr.addFlashAttribute("errorTitle", "Error");
|
||||
attr.addFlashAttribute("error", e.getMessage());
|
||||
attr.addFlashAttribute("params", params);
|
||||
} catch (ConstraintViolationException e) {
|
||||
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (validation error)");
|
||||
attr.addFlashAttribute("errorTitle", "Validation error");
|
||||
attr.addFlashAttribute("error", "See below for details");
|
||||
attr.addFlashAttribute("validationErrors", new ValidationErrorsContainer(e));
|
||||
attr.addFlashAttribute("params", params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (attr.getFlashAttributes().containsKey("error")) {
|
||||
if (create)
|
||||
|
@ -22,6 +22,10 @@ public class ValidationErrorsContainer {
|
||||
return errors.getOrDefault(name, new ArrayList<>());
|
||||
}
|
||||
|
||||
public boolean hasErrors(String name) {
|
||||
return forField(name).size() > 0;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return errors.isEmpty();
|
||||
}
|
||||
|
@ -2,6 +2,10 @@
|
||||
color: red;
|
||||
}
|
||||
|
||||
div.invalid input {
|
||||
border-color: red !important;
|
||||
}
|
||||
|
||||
.separator-light {
|
||||
opacity: 25%;
|
||||
}
|
||||
|
@ -30,7 +30,8 @@
|
||||
<h3 class="fw-bold mb-4" th:text="${create ? schema.getJavaClass().getSimpleName() : object.getDisplayName()}"></h3>
|
||||
<form class="form" enctype="multipart/form-data" method="post" th:action="|/${dbadmin_baseUrl}/model/${className}/create|">
|
||||
<input type="hidden" name="__dbadmin_create" th:value="${create}">
|
||||
<div th:each="field : ${schema.getSortedFields(false)}" class="mt-2">
|
||||
<div th:each="field : ${schema.getSortedFields(false)}" class="mt-2"
|
||||
th:classAppend="|${validationErrors != null && validationErrors.hasErrors(field.getJavaName()) ? 'invalid' : ''}|">
|
||||
<label th:for="|__id_${field.getName()}|" class="mb-1 fw-bold">
|
||||
<span th:if="${!field.isNullable() && !field.isPrimaryKey()}">
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user