This library renders HTML form from JSON Schema field tags of a Go structure.
It is based on github.com/jsonform/jsonform JS library.
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","ace"formTitleexample"Submit"readOnlyexample"true"prependexample"I feel"appendexample"today"noTitleexample"true"htmlClassexample"usermood"fieldHTMLClassexample"input-xxlarge"placeholderexample"incredibly and admirably great"inlineTitleexample"Check this box if you are over 18"activeClassexample"btn-success", button mode for radio buttonshelpValueexample"<strong>Click me!</strong>"