Fix: improve EditTransaction form handling and validation
- Add hidden fields for immutable transaction properties to preserve values during form submission - Make category field optional by removing validation for empty values - Simplify category input handling by removing duplicate hidden field - Clean up JavaScript by using proper element IDs instead of querySelector 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,12 @@
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<input type="hidden" asp-for="Transaction.Id" />
|
<input type="hidden" asp-for="Transaction.Id" />
|
||||||
|
<input type="hidden" asp-for="Transaction.Date" />
|
||||||
|
<input type="hidden" asp-for="Transaction.Name" />
|
||||||
|
<input type="hidden" asp-for="Transaction.Memo" />
|
||||||
|
<input type="hidden" asp-for="Transaction.Amount" />
|
||||||
|
<input type="hidden" asp-for="Transaction.CardLabel" />
|
||||||
|
<input type="hidden" asp-for="Transaction.AccountLabel" />
|
||||||
|
|
||||||
<!-- Read-only fields -->
|
<!-- Read-only fields -->
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
@@ -89,11 +95,10 @@
|
|||||||
<div id="customCategoryInput" style="display: @(Model.UseCustomCategory ? "block" : "none")">
|
<div id="customCategoryInput" style="display: @(Model.UseCustomCategory ? "block" : "none")">
|
||||||
<input asp-for="Transaction.Category"
|
<input asp-for="Transaction.Category"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
|
id="categoryInput"
|
||||||
placeholder="Enter custom category" />
|
placeholder="Enter custom category" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input type="hidden" asp-for="Transaction.Category" id="categoryHidden" />
|
|
||||||
|
|
||||||
<div class="form-text">Select a category or create a new one</div>
|
<div class="form-text">Select a category or create a new one</div>
|
||||||
<span asp-validation-for="Transaction.Category" class="text-danger"></span>
|
<span asp-validation-for="Transaction.Category" class="text-danger"></span>
|
||||||
</div>
|
</div>
|
||||||
@@ -261,16 +266,15 @@
|
|||||||
<script>
|
<script>
|
||||||
function handleCategoryChange() {
|
function handleCategoryChange() {
|
||||||
const select = document.getElementById('categorySelect');
|
const select = document.getElementById('categorySelect');
|
||||||
const customInput = document.getElementById('customCategoryInput');
|
const customInputDiv = document.getElementById('customCategoryInput');
|
||||||
const hiddenInput = document.getElementById('categoryHidden');
|
const categoryInput = document.getElementById('categoryInput');
|
||||||
const categoryInput = document.querySelector('input[name="Transaction.Category"]');
|
|
||||||
|
|
||||||
if (select.value === '__custom__') {
|
if (select.value === '__custom__') {
|
||||||
customInput.style.display = 'block';
|
customInputDiv.style.display = 'block';
|
||||||
categoryInput.value = '';
|
categoryInput.value = '';
|
||||||
categoryInput.focus();
|
categoryInput.focus();
|
||||||
} else {
|
} else {
|
||||||
customInput.style.display = 'none';
|
customInputDiv.style.display = 'none';
|
||||||
categoryInput.value = select.value;
|
categoryInput.value = select.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,12 @@ namespace MoneyMap.Pages
|
|||||||
|
|
||||||
public async Task<IActionResult> OnPostAsync()
|
public async Task<IActionResult> OnPostAsync()
|
||||||
{
|
{
|
||||||
|
// Remove Category from model state validation if it's empty (category is optional)
|
||||||
|
if (string.IsNullOrWhiteSpace(Transaction.Category))
|
||||||
|
{
|
||||||
|
ModelState.Remove("Transaction.Category");
|
||||||
|
}
|
||||||
|
|
||||||
// Remove Notes from model state validation if it's empty
|
// Remove Notes from model state validation if it's empty
|
||||||
if (string.IsNullOrWhiteSpace(Transaction.Notes))
|
if (string.IsNullOrWhiteSpace(Transaction.Notes))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user