Skip to content
Snippets Groups Projects
Commit ba233cdf authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#338 - allow empty amount; send tags to server

parent b146ed0c
Branches
Tags
No related merge requests found
......@@ -3,26 +3,27 @@ package de.deadlocker8.budgetmaster.templates;
import de.deadlocker8.budgetmaster.controller.BaseController;
import de.deadlocker8.budgetmaster.settings.SettingsService;
import de.deadlocker8.budgetmaster.transactions.Transaction;
import de.deadlocker8.budgetmaster.transactions.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@Controller
public class TemplateController extends BaseController
{
private final TemplateService templateService;
private final SettingsService settingsService;
private final TransactionService transactionService;
@Autowired
public TemplateController(TemplateService templateService, SettingsService settingsService)
public TemplateController(TemplateService templateService, SettingsService settingsService, TransactionService transactionService)
{
this.templateService = templateService;
this.settingsService = settingsService;
this.transactionService = transactionService;
}
@GetMapping("/templates")
......@@ -54,10 +55,10 @@ public class TemplateController extends BaseController
@RequestParam(value = "isPayment", required = false) boolean isPayment)
{
//TODO: handle BindingResult and errors
if(transaction.getTags() == null)
{
transaction.setTags(new ArrayList<>());
}
transactionService.handleAmount(transaction, isPayment);
transactionService.handleTags(transaction);
final Template template = new Template(templateName, transaction);
templateService.getRepository().save(template);
......
......@@ -9,6 +9,14 @@ $(document).ready(function()
{
$('#buttonSaveAsTemplate').click(function()
{
let isValidForm = validateForm(true);
if(!isValidForm)
{
$('#modalCreateFromTransaction').modal('close');
// TODO: toast
return;
}
$.ajax({
type: 'GET',
url: $("#buttonSaveAsTemplate").attr("data-url"),
......
......@@ -242,27 +242,41 @@ let transactionRepeatingEndAfterXTimesInputID = "#transaction-repeating-end-afte
AMOUNT_REGEX = new RegExp("^-?\\d+(,\\d+)?(\\.\\d+)?$");
ALLOWED_CHARACTERS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", "."];
function validateAmount(text)
function validateAmount(text, allowEmpty=false)
{
let id = "transaction-amount";
if(allowEmpty && text.length === 0)
{
removeTooltip(id);
let amount = parseFloat(text.replace(",", ".")) * 100;
document.getElementById("hidden-" + id).value = amount.toFixed(0);
return true;
}
if(text.match(AMOUNT_REGEX) == null)
{
addTooltip(id, amountValidationMessage);
document.getElementById("hidden-" + id).value = "";
return false;
}
else
{
removeTooltip(id);
let amount = parseFloat(text.replace(",", ".")) * 100;
document.getElementById("hidden-" + id).value = amount.toFixed(0);
return true;
}
}
function validateForm()
function validateForm(allowEmptyAmount=false)
{
// amount
validateAmount($('#transaction-amount').val());
let isValidAmount = validateAmount($('#transaction-amount').val(), allowEmptyAmount);
if(!isValidAmount)
{
return false;
}
// description
let description = document.getElementById('transaction-description').value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment