mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
Update 2 files
- /src/main/java/com/example/demo/controller/RecipeUploadController.java - /src/main/resources/templates/create-recipe.html
This commit is contained in:
@@ -129,7 +129,7 @@ function addIngredient(data = {}) {
|
||||
row.className = 'dynamic-row';
|
||||
row.innerHTML = `
|
||||
<input type="text" class="ing-name" placeholder="Ingredient (e.g. Sugar)" value="${escapeHtml(data.ingredientName || '')}">
|
||||
<input type="text" class="ing-qty" placeholder="Qty (e.g. 3)" value="${data.quantity ?? ''}">
|
||||
<input type="text" class="ing-qty" placeholder="Qty (e.g. 2, 1/2, 1 1/2)" value="${escapeHtml(data.quantity ?? '')}">
|
||||
<input type="text" class="ing-unit" placeholder="Unit (e.g. tbsp)" value="${escapeHtml(data.unit || '')}">
|
||||
<input type="text" class="ing-notes" placeholder="Notes (optional)" value="${escapeHtml(data.notes || '')}">
|
||||
<button class="btn-remove" title="Remove" type="button">✕</button>`;
|
||||
@@ -283,7 +283,7 @@ function showMessage(message, isError = false) {
|
||||
function showSuccessAndRedirect(message) {
|
||||
showMessage(message, false);
|
||||
setTimeout(() => {
|
||||
window.location.href = '/';
|
||||
window.location.href = '/explore';
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
@@ -317,6 +317,29 @@ function clearAllErrors() {
|
||||
box.textContent = '';
|
||||
}
|
||||
|
||||
function isValidQuantity(val) {
|
||||
const cleaned = String(val ?? '').replace(/\s+/g, ' ').trim();
|
||||
|
||||
if (cleaned === '') return true;
|
||||
|
||||
if (/^\d+(\.\d+)?$/.test(cleaned)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (/^\d+\/\d+$/.test(cleaned)) {
|
||||
const [numerator, denominator] = cleaned.split('/');
|
||||
return Number(numerator) >= 0 && Number(denominator) > 0;
|
||||
}
|
||||
|
||||
if (/^\d+\s\d+\/\d+$/.test(cleaned)) {
|
||||
const [whole, fraction] = cleaned.split(' ');
|
||||
const [numerator, denominator] = fraction.split('/');
|
||||
return Number(whole) >= 0 && Number(numerator) >= 0 && Number(denominator) > 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
let valid = true;
|
||||
|
||||
@@ -363,6 +386,7 @@ function validateForm() {
|
||||
|
||||
const ingredientRows = [...document.querySelectorAll('#ingredients-container .dynamic-row')];
|
||||
const filledIngredientRows = ingredientRows.filter(row => row.querySelector('.ing-name').value.trim() !== '');
|
||||
|
||||
if (filledIngredientRows.length === 0 && ingredientRows.length > 0) {
|
||||
showError(ingredientRows[0].querySelector('.ing-name'), 'Add at least one ingredient');
|
||||
valid = false;
|
||||
@@ -371,6 +395,7 @@ function validateForm() {
|
||||
filledIngredientRows.forEach(row => {
|
||||
const nameInput = row.querySelector('.ing-name');
|
||||
const qtyInput = row.querySelector('.ing-qty');
|
||||
|
||||
clearError(nameInput);
|
||||
clearError(qtyInput);
|
||||
|
||||
@@ -384,17 +409,6 @@ function validateForm() {
|
||||
showError(qtyInput, 'Enter a valid quantity (e.g. 2, 1.5, 1/2, 1 1/2) or leave blank');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function isValidQuantity(val) {
|
||||
if (val === '') return true;
|
||||
if (!isNaN(Number(val))) return true;
|
||||
if (/^\d+\/\d+$/.test(val)) return true;
|
||||
if (/^\d+\s+\d+\/\d+$/.test(val)) return true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
const stepAreas = [...document.querySelectorAll('#steps-container textarea')];
|
||||
|
||||
Reference in New Issue
Block a user