Highlighting inputs with errors

This commit is contained in:
Francesco 2023-10-10 22:19:56 +02:00
parent 13b45e62b8
commit c17d80514a
5 changed files with 37 additions and 50 deletions

View File

@ -439,68 +439,46 @@ public class DefaultDbAdminController {
pkValue = null; pkValue = null;
} }
if (pkValue == null) { try {
try { if (pkValue == null) {
Object newPrimaryKey = repository.create(schema, params, files, pkValue); Object newPrimaryKey = repository.create(schema, params, files, pkValue);
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams); repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
pkValue = newPrimaryKey.toString(); pkValue = newPrimaryKey.toString();
attr.addFlashAttribute("message", "Item created successfully."); attr.addFlashAttribute("message", "Item created successfully.");
saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE", schema.getClassName())); saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE", schema.getClassName()));
} catch (DataIntegrityViolationException | UncategorizedSQLException | IdentifierGenerationException e) { } else {
attr.addFlashAttribute("errorTitle", "Unable to INSERT row"); Optional<DbObject> object = repository.findById(schema, pkValue);
attr.addFlashAttribute("error", e.getMessage());
attr.addFlashAttribute("params", params); if (!object.isEmpty()) {
} catch (ConstraintViolationException e) { if (create) {
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (validation error)"); attr.addFlashAttribute("errorTitle", "Unable to create item");
attr.addFlashAttribute("error", "See below for details"); attr.addFlashAttribute("error", "Item with id " + object.get().getPrimaryKeyValue() + " already exists.");
attr.addFlashAttribute("validationErrors", new ValidationErrorsContainer(e)); attr.addFlashAttribute("params", params);
attr.addFlashAttribute("params", params); } else {
}
} else {
Optional<DbObject> object = repository.findById(schema, pkValue);
if (!object.isEmpty()) {
if (create) {
attr.addFlashAttribute("errorTitle", "Unable to create item");
attr.addFlashAttribute("error", "Item with id " + object.get().getPrimaryKeyValue() + " already exists.");
attr.addFlashAttribute("params", params);
} else {
try {
repository.update(schema, params, files); repository.update(schema, params, files);
repository.attachManyToMany(schema, pkValue, multiValuedParams); repository.attachManyToMany(schema, pkValue, multiValuedParams);
attr.addFlashAttribute("message", "Item saved successfully."); attr.addFlashAttribute("message", "Item saved successfully.");
saveAction(new UserAction(schema.getTableName(), pkValue, "EDIT", schema.getClassName())); 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 {
} else { Object newPrimaryKey = repository.create(schema, params, files, pkValue);
try { repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
Object newPrimaryKey = repository.create(schema, params, files, pkValue); attr.addFlashAttribute("message", "Item created successfully");
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams); saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE", schema.getClassName()));
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("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);
} }
} }
} catch (DataIntegrityViolationException | UncategorizedSQLException | IdentifierGenerationException e) {
attr.addFlashAttribute("errorTitle", "Error");
attr.addFlashAttribute("error", e.getMessage());
attr.addFlashAttribute("params", params);
} catch (ConstraintViolationException e) {
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 (attr.getFlashAttributes().containsKey("error")) {
if (create) if (create)
return "redirect:/" + properties.getBaseUrl() + "/model/" + schema.getClassName() + "/create"; return "redirect:/" + properties.getBaseUrl() + "/model/" + schema.getClassName() + "/create";

View File

@ -22,6 +22,10 @@ public class ValidationErrorsContainer {
return errors.getOrDefault(name, new ArrayList<>()); return errors.getOrDefault(name, new ArrayList<>());
} }
public boolean hasErrors(String name) {
return forField(name).size() > 0;
}
public boolean isEmpty() { public boolean isEmpty() {
return errors.isEmpty(); return errors.isEmpty();
} }

View File

@ -2,6 +2,10 @@
color: red; color: red;
} }
div.invalid input {
border-color: red !important;
}
.separator-light { .separator-light {
opacity: 25%; opacity: 25%;
} }

View File

@ -34,7 +34,7 @@
type="text" type="text"
th:value="${value}" th:value="${value}"
th:name="${name}" th:name="${name}"
class="form-control " th:id="|__id_${field.getName()}|" class="form-control" th:id="|__id_${field.getName()}|"
th:classAppend="|${(field.isPrimaryKey() && object != null) || th:classAppend="|${(field.isPrimaryKey() && object != null) ||
(field.isReadOnly() && !create) ? 'disable' : ''}|" (field.isReadOnly() && !create) ? 'disable' : ''}|"

View File

@ -30,7 +30,8 @@
<h3 class="fw-bold mb-4" th:text="${create ? schema.getJavaClass().getSimpleName() : object.getDisplayName()}"></h3> <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|"> <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}"> <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"> <label th:for="|__id_${field.getName()}|" class="mb-1 fw-bold">
<span th:if="${!field.isNullable() && !field.isPrimaryKey()}"> <span th:if="${!field.isNullable() && !field.isPrimaryKey()}">
* *