Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Show form errors on the nutritional values calculator on ingredient page

Roland Geidere587379b2674
diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.html
index 86ce67d4a..3d90294c6 100644
--- a/wger/nutrition/templates/ingredient/view.html
+++ b/wger/nutrition/templates/ingredient/view.html
@@ -12,6 +12,17 @@ function update_value() {
form_data = $('#nutritional-values-form').serializeArray();
$.post("{% url 'ingredient-get-values' ingredient.id %}", form_data, function(data) {
+
+ // Show any validation errors
+ $('#calculator-errors').html('');
+ if (data['errors'])
+ {
+ $.each(data['errors'], function(index, value) {
+ $('#calculator-errors').append('<p class="ym-message">'+value+'</p>');
+ });
+ }
+
+ // Replace the nutritional values
$('#value-energy').html(parseFloat(data['energy']).toFixed(2));
$('#value-protein').html(parseFloat(data['protein']).toFixed(2));
$('#value-carbohydrates').html(parseFloat(data['carbohydrates']).toFixed(2));
@@ -149,6 +160,8 @@ $(document).ready(function() {
method="post"
action="{% url 'ingredient-get-values' ingredient.id %}"
id="nutritional-values-form">
+ <div id="calculator-errors" class="ym-error"></div>
+
{% csrf_token %}
<input type="hidden" value="{{ingredient.id}}" name="ingredient"></input>
diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.py
index dbbc297c5..22f017e47 100644
--- a/wger/nutrition/views/ingredient.py
+++ b/wger/nutrition/views/ingredient.py
@@ -346,7 +346,8 @@ def ajax_get_ingredient_values(request, pk):
'fat': 0,
'fat_saturated': 0,
'fibres': 0,
- 'sodium': 0}
+ 'sodium': 0,
+ 'errors': []}
ingredient = get_object_or_404(Ingredient, pk=pk)
if request.method == 'POST':
@@ -367,7 +368,7 @@ def ajax_get_ingredient_values(request, pk):
result = item.get_nutritional_values()
else:
- logger.debug(form.errors)
+ result['errors'] = form.errors
return HttpResponse(json.dumps(result, cls=helpers.DecimalJsonEncoder),
'application/json')