コード例 #1
0
ファイル: validators_test.go プロジェクト: jwulf/zedlist
func TestBirthDateValidator(t *testing.T) {
	var now = time.Now()
	var yearsAgo = func(yrs int) time.Time {
		n := time.Now()
		nowAFter := n.AddDate(18, 1, 1)
		dur := nowAFter.Sub(n)
		return n.Add(-dur)

	}
	Form := gforms.DefineForm(gforms.NewFields(
		gforms.NewDateTimeField(
			"date",
			time.RFC822,
			gforms.Validators{
				BirthDateValidator{Limit: 18, Message: msgAge},
			},
		),
	))

	vars := url.Values{"date": {now.Format(time.RFC822)}}
	req1, err := http.NewRequest("POST", "/", strings.NewReader(vars.Encode()))
	if err != nil {
		t.Error(err)
	}
	req1.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	form := Form(req1)
	if form.IsValid() {
		t.Error("Expected some errors")
	}

	vars = url.Values{"date": {yearsAgo(18).Format(time.RFC822)}}
	req2, err := http.NewRequest("POST", "/", strings.NewReader(vars.Encode()))
	if err != nil {
		t.Error(err)
	}
	req2.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	form = Form(req2)
	if !form.IsValid() {
		t.Error(form.Errors())
	}
}
コード例 #2
0
ファイル: auth.go プロジェクト: jwulf/zedlist
// LoginForm returns a gform model for Login.
func (f *Form) LoginForm() gforms.ModelForm {
	var attrs = map[string]string{
		"class": "input-large",
	}
	return gforms.DefineModelForm(Login{}, gforms.NewFields(
		gforms.NewTextField(
			"email",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
				gforms.EmailValidator(f.tr.T(msgEmail)),
			},
			gforms.BaseTextWidget("email", attrs),
		),
		gforms.NewTextField(
			"password",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
				gforms.MinLengthValidator(6, f.tr.T(msgMinLength, 6)),
			},
			gforms.PasswordInputWidget(attrs),
		),
	))
}
コード例 #3
0
ファイル: validators_test.go プロジェクト: jwulf/zedlist
func TestEqualValidator(t *testing.T) {
	var msg = "%s should equal %s"
	eForm := gforms.DefineForm(gforms.NewFields(
		gforms.NewTextField(
			"first",
			gforms.Validators{
				gforms.Required(),
			},
		),
		gforms.NewTextField(
			"second",
			gforms.Validators{
				EqualValidator{To: "first", Message: msg},
			},
		),
	))

	vars := url.Values{
		"first":  {"hello"},
		"second": {"world"},
	}
	req1, err := http.NewRequest("POST", "/", strings.NewReader(vars.Encode()))
	if err != nil {
		t.Error(err)
	}
	req1.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	form := eForm(req1)
	if form.IsValid() {
		t.Error("expected errors")
	}
	errMsg := form.Errors().Get("second")[0]
	expMsg := fmt.Sprintf(msg, "second", "first")
	if errMsg != expMsg {
		t.Errorf(" expected %s got %s", expMsg, errMsg)
	}
}
コード例 #4
0
ファイル: job.go プロジェクト: jwulf/zedlist
// JobForm implements gform.ModelForm interface.
func (f *Form) JobForm() gforms.ModelForm {
	titleAttrs := map[string]string{
		"id": "job-title",
	}
	descAttr := map[string]string{
		"id": "job-description",
	}
	return gforms.DefineModelForm(JobForm{}, gforms.NewFields(
		gforms.NewTextField(
			"title",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.TextInputWidget(titleAttrs),
		),
		gforms.NewTextField(
			"description",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.TextAreaWidget(descAttr),
		),
	))
}
コード例 #5
0
ファイル: auth.go プロジェクト: jwulf/zedlist
// RegisterForm implements gforms.ModelForm interface for Registration form.
func (f *Form) RegisterForm() gforms.ModelForm {
	var birtdateAttrs = map[string]string{
		"id":    "birthdate",
		"class": "input-large",
	}
	var inputAttrs = map[string]string{
		"class": "input-large",
	}
	return gforms.DefineModelForm(Register{}, gforms.NewFields(
		gforms.NewTextField(
			"first_name",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.TextInputWidget(inputAttrs),
		),
		gforms.NewTextField(
			"last_name",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.TextInputWidget(inputAttrs),
		),
		gforms.NewTextField(
			"middle_name",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.TextInputWidget(inputAttrs),
		),
		gforms.NewTextField(
			"email",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
				gforms.EmailValidator(f.tr.T(msgEmail)),
			},
			gforms.BaseTextWidget("email", inputAttrs),
		),
		gforms.NewTextField(
			"password",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
				gforms.MinLengthValidator(6, f.tr.T(msgMinLength, 6)),
			},
			gforms.PasswordInputWidget(inputAttrs),
		),
		gforms.NewTextField(
			"confirm_password",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
				gforms.MinLengthValidator(6, f.tr.T(msgMinLength, 6)),
				EqualValidator{To: "password", Message: f.tr.T(msgEqual)},
			},
			gforms.PasswordInputWidget(inputAttrs),
		),
		gforms.NewTextField(
			"gender",
			gforms.Validators{
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.SelectWidget(
				inputAttrs,
				func() gforms.SelectOptions {
					return gforms.StringSelectOptions([][]string{
						{"Select...", "", "true", "false"},
						{"Male", "0", "false", "false"},
						{"Female", "1", "false", "false"},
						{"Zombie", "2", "false", "true"},
					})
				},
			),
		),

		gforms.NewDateTimeField(
			"birth_date",
			settings.App.BirthDateFormat,
			gforms.Validators{
				BirthDateValidator{Limit: settings.App.MinimumAge, Message: f.tr.T(msgAge, settings.App.MinimumAge)},
				gforms.Required(f.tr.T(msgRequired)),
			},
			gforms.BaseTextWidget("text", birtdateAttrs),
		),
	))
}