               CoffeeAlerts.success(@_recordName() + " updated")
               @fetchRecordCount()
       else
-        CoffeeAlerts.error("Error could update " + @_recordName())
+        CoffeeAlerts.error("Error could update " + @_recordName() + " " + @errorMessage)
 
 
   newRecord: ->
-    CoffeeModal.form(@formTemplate, @formData('insert'), @saveNewRecord, 'New ' + @_recordName().capitalize())
-
-
-  saveNewRecord: (yesNo, rec) =>
+    CoffeeModal.form(@formTemplate, @formData('insert'), @insertRecord, 'New ' + @_recordName().capitalize())
+
+
+  insertRecord: (yesNo, rec) =>
+    @errorMessage = ''
     if yesNo
-      if @collection().insertOk(rec) and @checkRequiredFields(rec)
+      if @collection().insertOk(rec) and @checkFields(rec, 'insert')
         if @collection().methodOnInsert
           Meteor.call @collection().methodOnInsert, rec, (error) =>
             if error

  loadingTemplate : 'ironTableLoading'
  defaultSelect   : {}
  showFilter      : false

  _subscriptionComplete = false
  

      @fetchRecordCount()


  checkFields: (rec, type="insert") ->
    @errorMessage = ''
    for key, col of @_cols()
      dataKey = col.dataKey or key
      if col.required and (not rec[dataKey]? or rec[dataKey] is '')
        col.header = (col.header || key).capitalize()
        @errorMessage = "#{col.header} is required"
        return false
      else if type is 'insert' and col.onInsert?
        rec[dataKey] = col.onInsert()
      else if type is 'update' and col.onUpdate?
        rec[dataKey] = col.onUpdate()
    true


