# HG changeset patch
# User Augusto Mauch <augusto.mauch@openbravo.com>
# Date 1558545694 -7200
#      Wed May 22 19:21:34 2019 +0200
# Node ID 1ac425d171c3d9f9ee3df661515551f3951a065a
# Parent  898c207324d9e827220c298d68667eccd6d00afe
Fixes issue 39865: Do not create line if mandatory fields of current are empty

In the view grid, if in a new line there are mandatory fields not filled in, it should not be
possible to create a new line.

To prevent that, the updateState function of the new line toolbar button has been updated, and
the doCellEditEnd function of ob-view-grid also takes it into account.

diff --git a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
@@ -3495,7 +3495,11 @@
     }
 
     // If leaving the row...
-    if (editCompletionEvent === 'enter' || editCompletionEvent === 'arrow_up' || editCompletionEvent === 'arrow_down') {
+    if (newRow) {
+      // do not leave the row if the row is new and not all mandatory fields have been set
+      if (editForm && editForm.isNew && !editForm.allRequiredFieldsSet()) {
+        return;
+      }
       // See issue https://issues.openbravo.com/view.php?id=19830
       if (this.view.standardWindow.getDirtyEditForm()) {
         this.view.standardWindow.getDirtyEditForm().validateForm();
diff --git a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js
@@ -122,8 +122,11 @@
     prompt: OB.I18N.getLabel('OBUIAPP_NewRow'),
     updateState: function () {
       var view = this.view,
-          selectedRecords = view.viewGrid.getSelectedRecords();
-      this.setDisabled(view.viewGrid.isGrouped || view.isShowingForm || view.readOnly || view.editOrDeleteOnly || view.singleRecord || !view.hasValidState() || (selectedRecords && selectedRecords.length > 1) || view.isShowingTree || !view.roleCanCreateRecords());
+          selectedRecords = view.viewGrid.getSelectedRecords(),
+          form = view.viewGrid.getEditForm(),
+          allFieldsSet = form && form.allRequiredFieldsSet(),
+          isNew = form && form.isNew;
+      this.setDisabled(view.viewGrid.isGrouped || view.isShowingForm || view.readOnly || view.editOrDeleteOnly || view.singleRecord || !view.hasValidState() || (selectedRecords && selectedRecords.length > 1) || view.isShowingTree || !view.roleCanCreateRecords() || (isNew && !allFieldsSet));
     },
     keyboardShortcutId: 'ToolBar_NewRow'
   },
