From 8c6e3db774dec439c5224f3b534af65a8cf54960 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Sun, 26 Jun 2022 12:25:42 +1000
Subject: [PATCH] Bug fix for field validation on child forms (#3258)

* Bug fix for field validation on child forms

- If a child form is launched which conatins numerical inputs, field validation fails
- Not taking the "level" parameter into account when looking for the field

* trim long description strings in modal forms
---
 InvenTree/templates/js/translated/forms.js    |  5 ++--
 .../js/translated/model_renderers.js          | 24 ++++++++++++++-----
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js
index fb9f422b67..69a4c5460d 100644
--- a/InvenTree/templates/js/translated/forms.js
+++ b/InvenTree/templates/js/translated/forms.js
@@ -938,7 +938,7 @@ function getFormFieldElement(name, options) {
 /*
  * Check that a "numerical" input field has a valid number in it.
  * An invalid number is expunged at the client side by the getFormFieldValue() function,
- * which means that an empty string '' is sent to the server if the number is not valud.
+ * which means that an empty string '' is sent to the server if the number is not valid.
  * This can result in confusing error messages displayed under the form field.
  *
  * So, we can invalid numbers and display errors *before* the form is submitted!
@@ -947,7 +947,8 @@ function validateFormField(name, options) {
 
     if (getFormFieldElement(name, options)) {
 
-        var el = document.getElementById(`id_${name}`);
+        var field_name = getFieldName(name, options);
+        var el = document.getElementById(`id_${field_name}`);
 
         if (el.validity.valueMissing) {
             // Accept empty strings (server will validate)
diff --git a/InvenTree/templates/js/translated/model_renderers.js b/InvenTree/templates/js/translated/model_renderers.js
index 7be7c2d656..5f3d9e8279 100644
--- a/InvenTree/templates/js/translated/model_renderers.js
+++ b/InvenTree/templates/js/translated/model_renderers.js
@@ -31,6 +31,18 @@
  */
 
 
+/*
+ * Trim the supplied string to ensure the string length is limited to the provided value
+ */
+function trim(data, max_length=100) {
+    if (data.length > max_length) {
+        data = data.slice(0, max_length - 3) + '...';
+    }
+
+    return data;
+}
+
+
 // Should the ID be rendered for this string
 function renderId(title, pk, parameters={}) {
 
@@ -55,7 +67,7 @@ function renderCompany(name, data, parameters={}, options={}) {
 
     var html = select2Thumbnail(data.image);
 
-    html += `<span><b>${data.name}</b></span> - <i>${data.description}</i>`;
+    html += `<span><b>${data.name}</b></span> - <i>${trim(data.description)}</i>`;
 
     html += renderId('{% trans "Company ID" %}', data.pk, parameters);
 
@@ -141,7 +153,7 @@ function renderStockLocation(name, data, parameters={}, options={}) {
     }
 
     if (render_description && data.description) {
-        html += ` - <i>${data.description}</i>`;
+        html += ` - <i>${trim(data.description)}</i>`;
     }
 
     html += renderId('{% trans "Location ID" %}', data.pk, parameters);
@@ -177,7 +189,7 @@ function renderPart(name, data, parameters={}, options={}) {
     html += ` <span>${data.full_name || data.name}</span>`;
 
     if (data.description) {
-        html += ` - <i><small>${data.description}</small></i>`;
+        html += ` - <i><small>${trim(data.description)}</small></i>`;
     }
 
     var stock_data = '';
@@ -256,7 +268,7 @@ function renderPurchaseOrder(name, data, parameters={}, options={}) {
     }
 
     if (data.description) {
-        html += ` - <em>${data.description}</em>`;
+        html += ` - <em>${trim(data.description)}</em>`;
     }
 
     html += renderId('{% trans "Order ID" %}', data.pk, parameters);
@@ -282,7 +294,7 @@ function renderSalesOrder(name, data, parameters={}, options={}) {
     }
 
     if (data.description) {
-        html += ` - <em>${data.description}</em>`;
+        html += ` - <em>${trim(data.description)}</em>`;
     }
 
     html += renderId('{% trans "Order ID" %}', data.pk, parameters);
@@ -319,7 +331,7 @@ function renderPartCategory(name, data, parameters={}, options={}) {
     var html = `<span>${level}${data.pathstring}</span>`;
 
     if (data.description) {
-        html += ` - <i>${data.description}</i>`;
+        html += ` - <i>${trim(data.description)}</i>`;
     }
 
     html += renderId('{% trans "Category ID" %}', data.pk, parameters);