# HG changeset patch
# User Augusto Mauch <augusto.mauch@openbravo.com>
# Date 1419409451 -3600
#      Wed Dec 24 09:24:11 2014 +0100
# Node ID 5540170892d2db5e1af5d832e032af5fd05c09ab
# Parent  95dd177fbbc7f18d448b3e31cc12b67dbc0cb87d
Fixes issue 28469: Masked string validation is properly done in editable grid

There were several problems that were preventing the handling of masked string validations from working properly:
1- The masked string validations were not being performed in grid view, due to this code in the validate function:

      if (this.form && this.form.grid && this.form.grid._showingEditor) {
        return;
      }

   That code was put there to prevent doing unneeded validations when a new record was created in the grid view (see issue https://issues.openbravo.com/view.php?id=19176). This issue remains fixed after re
moving these lines.

2- After fixing 1), the Save toolbar button was enabled in the grid view even if the row being edited had masked string validation errores. These happened because of a flaw in the logic to disable the butt
on:

     this.setDisabled(... && !hasErrors && ...);

   The button was being disabled when the editable form did not have errors, and enabled when the form had errors. Before fixing 1) this did not matter, as hasErrors was always false because the form item
was not being validated. This has been fixed by disabling the button when the form had validation errors.

3- Sometimes (i.e. when the validation was performed due to an autosave) the mask validation was not taken into account. This happened because the validation was added to the validation list of the test it
em, but not to the validation list of the grid field, that is where it was being taken from.

diff --git a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-text.js b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-text.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-text.js
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-text.js
@@ -39,6 +39,7 @@
   },
 
   resetMaskValidator: function (createNew) {
+    var gridField;
     if (this.maskValidator && this.validators) {
       this.validators.remove(this.maskValidator);
       delete this.maskValidator;
@@ -49,6 +50,11 @@
       this.validators = this.validators || [];
       this.validators.push(this.maskValidator);
     }
+    gridField = this.grid.getField(this.name);
+    if (gridField) {
+      // update the validators of the grid field, so that it is taken into account in the ListGrid.validateRowValues function
+      gridField.validators = this.validators;
+    }
   },
 
   createRegExpFromMask: function (mask) {
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
@@ -52,7 +52,7 @@
         form = view.viewGrid.getEditForm();
         editRow = view.viewGrid.getEditRow();
         hasErrors = view.viewGrid.rowHasErrors(editRow);
-        this.setDisabled(!(form.isNew && form.allRequiredFieldsSet()) && !hasErrors && (form.isSaving || form.readOnly || !view.hasValidState() || form.hasErrors() || !form.hasChanged || !form.allRequiredFieldsSet()));
+        this.setDisabled(!(form.isNew && form.allRequiredFieldsSet()) && (form.isSaving || form.readOnly || !view.hasValidState() || hasErrors || form.hasErrors() || !form.hasChanged || !form.allRequiredFieldsSet()));
       } else {
         this.setDisabled(true);
       }
diff --git a/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js b/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js
--- a/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js
+++ b/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js
@@ -553,9 +553,6 @@
 
     // prevent validation when we are showing the editor and moving
     // the focus around
-    if (this.form && this.form.grid && this.form.grid._showingEditor) {
-      return;
-    }
 
     if (this.preventValidation) {
       return;
