Require all fields to be filled out when creating a new user

This commit is contained in:
Jason Streifling 2024-02-24 12:10:34 +01:00
parent f3c8cd6fa5
commit f716e9f0b5
3 changed files with 21 additions and 8 deletions

View File

@ -61,6 +61,10 @@ func HandleAddUser(db *data.DB) http.HandlerFunc {
last := r.PostFormValue("last-name") last := r.PostFormValue("last-name")
role := r.PostFormValue("role") role := r.PostFormValue("role")
if inputsEmpty(user, pass, pass2, first, last, role) {
log.Println("inputsEmpty")
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
_, _, ok := checkUserStrings(user, first, last) _, _, ok := checkUserStrings(user, first, last)
if !ok { if !ok {
log.Println("checkUserStrings") log.Println("checkUserStrings")

View File

@ -1,5 +1,14 @@
package ui package ui
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
return len(user) == 0 ||
len(pass) == 0 ||
len(pass2) == 0 ||
len(first) == 0 ||
len(last) == 0 ||
len(role) == 0
}
func checkUserStrings(user, first, last string) (string, int, bool) { func checkUserStrings(user, first, last string) (string, int, bool) {
userLen := 15 userLen := 15
nameLen := 50 nameLen := 50

View File

@ -1,19 +1,19 @@
{{define "page-content"}} {{define "page-content"}}
<h2>Neuer Benutzer</h2> <h2>Neuer Benutzer</h2>
<form> <form>
<input name="username" placeholder="Benutzername" type="text" /> <input required name="username" placeholder="Benutzername" type="text" />
<input name="password" placeholder="Passwort" type="password" /> <input required name="password" placeholder="Passwort" type="password" />
<input name="password2" placeholder="Passwort wiederholen" type="password" /> <input required name="password2" placeholder="Passwort wiederholen" type="password" />
<input name="first-name" placeholder="Vorname" type="text" /> <input required name="first-name" placeholder="Vorname" type="text" />
<input name="last-name" placeholder="Nachname" type="text" /> <input required name="last-name" placeholder="Nachname" type="text" />
<label for="writer">Schreiber</label> <label for="writer">Schreiber</label>
<input id="writer" name="role" type="radio" value="writer" /> <input required id="writer" name="role" type="radio" value="writer" />
<label for="editor">Redakteur</label> <label for="editor">Redakteur</label>
<input id="editor" name="role" type="radio" value="editor" /> <input required id="editor" name="role" type="radio" value="editor" />
<label for="admin">Admin</label> <label for="admin">Admin</label>
<input id="admin" name="role" type="radio" value="admin" /> <input required id="admin" name="role" type="radio" value="admin" />
<input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" /> <input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" />
</form> </form>