Added early catching of invalid template values. Fixes #193

pull/233/head
Jordan Wright 2016-04-02 18:33:25 -05:00
parent 776e54c856
commit 83a17e8c2d
2 changed files with 40 additions and 1 deletions

View File

@ -1,7 +1,9 @@
package models package models
import ( import (
"bytes"
"errors" "errors"
"html/template"
"time" "time"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -33,7 +35,41 @@ func (t *Template) Validate() error {
case t.Text == "" && t.HTML == "": case t.Text == "" && t.HTML == "":
return ErrTemplateMissingParameter return ErrTemplateMissingParameter
} }
return nil var buff bytes.Buffer
// Test that the variables used in the template
// validate with no issues
td := struct {
Result
URL string
TrackingURL string
Tracker string
From string
}{
Result{
Email: "foo@bar.com",
FirstName: "Foo",
LastName: "Bar",
Position: "Test",
},
"http://foo.bar",
"http://foo.bar/track",
"<img src='http://foo.bar/track",
"John Doe <foo@bar.com>",
}
tmpl, err := template.New("html_template").Parse(t.HTML)
if err != nil {
return err
}
err = tmpl.Execute(&buff, td)
if err != nil {
return err
}
tmpl, err = template.New("text_template").Parse(t.Text)
if err != nil {
return err
}
err = tmpl.Execute(&buff, td)
return err
} }
// GetTemplates returns the templates owned by the given user. // GetTemplates returns the templates owned by the given user.

View File

@ -49,6 +49,9 @@ function save(idx) {
load() load()
dismiss() dismiss()
}) })
.error(function(data) {
modalError(data.responseJSON.message)
})
} else { } else {
// Submit the template // Submit the template
api.templates.post(template) api.templates.post(template)