Working create form
parent
cd7c75c707
commit
ec2c95efb6
@ -0,0 +1,13 @@
|
||||
package server
|
||||
|
||||
// Define a snippetCreateForm struct to represent the form data and validation
|
||||
// errors for the form fields. Note that all the struct fields are deliberately
|
||||
// exported (i.e. start with a capital letter). This is because struct fields
|
||||
// must be exported in order to be read by the html/template package when
|
||||
// rendering the template.
|
||||
type snippetCreateForm struct {
|
||||
Title string
|
||||
Content string
|
||||
Expires int
|
||||
FieldErrors map[string]string
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
{{define "title"}}Create a New Snippet{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<form action='/snippet/create' method='POST'>
|
||||
<div>
|
||||
<label>Title:</label>
|
||||
<!-- Use the `with` action to render the value of .Form.FieldErrors.title
|
||||
if it is not empty. -->
|
||||
{{with .Form.FieldErrors.title}}
|
||||
<label class='error'>{{.}}</label>
|
||||
{{end}}
|
||||
<input type='text' name='title' value="{{.Form.Title}}">
|
||||
</div>
|
||||
<div>
|
||||
<label>Content:</label>
|
||||
<!-- Likewise render the value of .Form.FieldErrors.content if it is not
|
||||
empty. -->
|
||||
{{with .Form.FieldErrors.content}}
|
||||
<label class='error'>{{.}}</label>
|
||||
{{end}}
|
||||
<textarea name='content'>{{.Form.Content}}</textarea>
|
||||
</div>
|
||||
<div>
|
||||
<label>Delete in:</label>
|
||||
<!-- And render the value of .Form.FieldErrors.expires if it is not empty. -->
|
||||
{{with .Form.FieldErrors.expires}}
|
||||
<label class='error'>{{.}}</label>
|
||||
{{end}}
|
||||
<input type='radio' name='expires' value='365' {{if (eq .Form.Expires 365)}}checked{{end}}> One Year
|
||||
<input type='radio' name='expires' value='7' {{if (eq .Form.Expires 7)}}checked{{end}}> One Week
|
||||
<input type='radio' name='expires' value='1' {{if (eq .Form.Expires 1)}}checked{{end}}> One Day
|
||||
</div>
|
||||
<div>
|
||||
<input type='submit' value='Publish snippet'>
|
||||
</div>
|
||||
</form>
|
||||
{{end}}
|
@ -1,5 +1,6 @@
|
||||
{{define "nav" -}}
|
||||
<nav>
|
||||
<a href='/'>Home</a>
|
||||
<a href='/snippet/create'>Create snippet</a>
|
||||
</nav>
|
||||
{{- end}}
|
Loading…
Reference in New Issue