rudimentary validation in xoslib
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xosHelper.js
index 8fa27a6..30835c7 100644 (file)
@@ -264,10 +264,22 @@ XOSDetailView = Marionette.ItemView.extend({
 
             save: function() {
                 this.app.hideError();
-                var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );\r
                 var data = Backbone.Syphon.serialize(this);\r
                 var that = this;\r
                 var isNew = !this.model.id;\r
+\r
+                /* although model.validate() is called automatically by\r
+                   model.save, we call it ourselves, so we can throw up our\r
+                   validation error before creating the infoMsg in the log\r
+                */\r
+                errors =  this.model.validate(data);\r
+                if (errors) {\r
+                    this.onFormDataInvalid(errors);\r
+                    return;\r
+                }\r
+\r
+                var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );\r
+\r
                 this.model.save(data, {error: function(model, result, xhr) { that.saveError(model,result,xhr,infoMsgId);},\r
                                        success: function(model, result, xhr) { that.saveSuccess(model,result,xhr,infoMsgId);}});\r
                 if (isNew) {\r
@@ -356,6 +368,19 @@ XOSDetailView = Marionette.ItemView.extend({
                     this.tabClick('#xos-nav-detail', 'detail');\r
               },\r
 \r
+            onFormDataInvalid: function(errors) {\r
+                var self=this;\r
+                var markErrors = function(value, key) {\r
+                    console.log("name='" + key + "'");\r
+                    var $inputElement = self.$el.find("[name='" + key + "']");\r
+                    var $inputContainer = $inputElement.parent();\r
+                    $inputContainer.find(".help-inline").remove();\r
+                    var $errorEl = $("<span>", {class: "help-inline error", text: value});\r
+                    $inputContainer.append($errorEl).addClass("error");\r
+                }\r
+                _.each(errors, markErrors);\r
+            },\r
+\r
 });\r
 
 /* XOSItemView