Skip to content

Commit

Permalink
Added validation method on forms, updated readme and added coveralls …
Browse files Browse the repository at this point in the history
…in travis build file
  • Loading branch information
Danzabar committed Nov 4, 2015
1 parent 7072ada commit db84563
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ go:
- 1.3.3
- 1.4.2

before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
script:
- go vet
- go test -v -cover -bench .
- $HOME/gopath/bin/goveralls -service=travis-ci
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
Forms
=====
[![Build Status](https://travis-ci.org/Danzabar/forms.svg)](https://travis-ci.org/Danzabar/forms)
[![Build Status](https://travis-ci.org/Danzabar/forms.svg)](https://travis-ci.org/Danzabar/forms) [![Coverage Status](https://coveralls.io/repos/Danzabar/forms/badge.svg?branch=master&service=github)](https://coveralls.io/github/Danzabar/forms?branch=master)

A little library that allows you to control web forms through Go code. Currently in development

## Usage
The following is a basic example of how to set up and use a go form

// Create the form
form := NewForm(http.Request)
form.Action = "/test/form/uri"
form.Method = "POST"

// Add fields
uname := &Field{
Name: "username",
Label: "Username",
Type: "text",
}

form.addField(uname)

### Output
The form struct will output the form tags, and each field can output the Html representation of itself, for example

form.open()

for _, field := range form.Fields {
field.output()
}

form.close()

### Validate
The form struct can also validate all fields it has, or you can validate individual fields...

// Upon adding a field the method will check the request
// object for the corresponding field value
form.validate()

// You can validate a single field as well
field.validate()

Both the form and field struct have the `Valid` boolean flag to signify whether they have passed validation or not

form.validate()

// true
return form.Valid

### Validation rules
Remember to add the validation you need to the field, otherwise the result of `validate` will always be true!

rule := &Required{
Err: "This field is required!"
}

field.addValidation(rule)

In the above example, if the field has no value and so fails validation it will add an entry into the fields `Error` list.
4 changes: 4 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Field struct {
Default string
// The actual value
Value string
// Boolean flag for when a field has errors or not
Valid bool
// A list of values to use for multi-value inputs
Values []string
// A list of validation items to add to this field
Expand Down Expand Up @@ -59,12 +61,14 @@ func (f *Field) addValidation(validation Validation) {

// Runs all the validation associated with this field
func (f *Field) validate() {
f.Valid = true
for _, rule := range f.Rules {
rule.setValue(f.Value)
rule.validate()

if !rule.isValid() {
f.addError(rule.getErr())
f.Valid = false
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Form struct {
Fields []*Field
// The current request
Request http.Request
// Boolean flag whether form is valid or not
Valid bool
}

// Creates the form struct
Expand All @@ -28,6 +30,7 @@ func NewForm(r http.Request) *Form {
Action: "",
Method: "POST",
Request: r,
Valid: false,
}
}

Expand All @@ -49,3 +52,17 @@ func (f Form) open() string {
func (f Form) close() string {
return fmt.Sprintf("</form>")
}

// Method to validate the whole form
func (f *Form) validate() bool {
f.Valid = true
for _, field := range f.Fields {
field.validate()

if !field.Valid {
f.Valid = false
}
}

return f.Valid
}
24 changes: 24 additions & 0 deletions form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ func (suite *FormTestSuite) TestAddFieldWithValue() {
assert.Equal(suite.T(), "value", field.Value)
}

func (suite *FormTestSuite) TestValidateAllFields() {

req := http.Request{
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader("field1=test")),
Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded;"}},
}

form := NewForm(req)
form.Action = "/test/uri"
form.Method = "POST"

field := &Field{
Name: "field1",
Label: "Field",
Type: "text",
}

form.addField(field)
valid := form.validate()

assert.Equal(suite.T(), true, valid)
}

func TestRunnerTestSuite(t *testing.T) {
suite.Run(t, new(FormTestSuite))
}

0 comments on commit db84563

Please sign in to comment.