| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package forms
- import (
- "net/http"
- "strconv"
- )
- /* strFromPtr converts string pointer to a string */
- func strFromPtr(str *string) (s string) {
- if str != nil {
- s = *str
- }
- return
- }
- /* Generate new CharField field with type text */
- func NewCharField(name string, value *string) *FormField {
- return &FormField{
- Name: name,
- Value: strFromPtr(value),
- Kind: "text",
- }
- }
- /* Generate new integer type field */
- func NewIntField(name string, value int) *FormField {
- field := FormField{
- Name: name,
- Value: strconv.Itoa(value),
- Kind: "text",
- }
- field.AddValidator(ValidInt)
- return &field
- }
- /* Generate new password field */
- func NewPasswordField(name string) *FormField {
- return &FormField{
- Name: name,
- Kind: "password",
- }
- }
- /* Generate a new checkbox field */
- func NewCheckboxField(name string, checked bool) *FormField {
- return &FormField{
- Name: name,
- Checked: checked,
- Kind: "checkbox",
- }
- }
- /* Generate new select field */
- func NewSelectField(name string, items []string, value string) *FormField {
- return &FormField{
- Name: name,
- Select: items,
- Value: value,
- Kind: "select",
- }
- }
- /* Generate a new CSRF field */
- func NewCsrfField(w http.ResponseWriter, r *http.Request, secure bool) *FormField {
- return &FormField{
- Name: csrfFieldName,
- Value: csrfToken(w, r, secure),
- Kind: "hidden",
- Sticky: true,
- Validators: ValidatorsList{
- ValidCSRF(r),
- },
- }
- }
- /* Generate a new hidden text field */
- func NewHiddenField(name string, value *string) *FormField {
- return &FormField{
- Name: name,
- Value: strFromPtr(value),
- Kind: "hidden",
- }
- }
|