This library renders HTML form from JSON Schema field tags of a Go structure.
See full example.
Define or instrument existing structure with JSON schema field tags and with form item field tags.
type User struct {
FirstName string `json:"firstName" required:"true" title:"First name" minLength:"3"`
LastName string `json:"lastName" required:"true" title:"Last name" minLength:"3"`
Locale string `json:"locale" title:"User locale" enum:"ru-RU,en-US"`
Age int `json:"age" title:"Age" minimum:"1"`
Status userStatus `json:"status" title:"Status"`
Bio string `json:"bio" title:"Bio" description:"A brief description of the person." formType:"textarea"`
}
Create form repository and add schemas.
jf := jsonform.NewRepository(&jsonschema.Reflector{})
err := jf.Add(User{})
Mount handlers with static assets and schemas to *web.Service
.
jf.Mount(s, "/json-form/")
Form can be rendered using ./form.html
and query
parameters.
{String} title - Title of the form.
{String} schemaName - Schema name.
{String} valueUrl - URL to fetch value.
{String} submitUrl - URL to submit form.
{String} submitMethod - HTTP method to use on form submit.
{Number} successStatus - Success HTTP status code to expect on submit.
Examples:
/json-form/form.html?title=Create%20user&schemaName=user&submitUrl=/users&submitMethod=POST&successStatus=201
/json-form/form.html?title=Edit%20user&schemaName=user&valueUrl=/user/1.json&submitUrl=/user/1.json&submitMethod=PUT&successStatus=204
For more user-friendly URLs, multiple forms on page and other page customizations you can use Render
to create
web pages with forms.
repo.Render(output.Writer, jsonform.Page{}, jsonform.Form{
Title: "Update User",
SubmitMethod: http.MethodPut,
SubmitURL: "/user/" + strconv.Itoa(input.ID) + ".json",
Value: user,
SuccessStatus: http.StatusNoContent,
}
formType
, values"textarea"
,"password"
,"wysihtml5"
,"submit"
,"color"
,"checkboxes"
,"radios"
,"fieldset"
,"help"
,"hidden"
formTitle
example"Submit"
readOnly
example"true"
prepend
example"I feel"
append
example"today"
noTitle
example"true"
htmlClass
example"usermood"
fieldHTMLClass
example"input-xxlarge"
placeholder
example"incredibly and admirably great"
inlineTitle
example"Check this box if you are over 18"
activeClass
example"btn-success"
, button mode for radio buttonshelpValue
example"<strong>Click me!</strong>"