Skip to content

Commit 1afafde

Browse files
committed
user/setting: preserve user input with validation error (gogs#1123)
1 parent ab634ce commit 1afafde

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

modules/context/context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ func (ctx *Context) HasValue(name string) bool {
7979
return ok
8080
}
8181

82-
// HTML calls Context.HTML and converts template name to string.
82+
// HTML responses template with given status.
8383
func (ctx *Context) HTML(status int, name base.TplName) {
8484
log.Trace("Template: %s", name)
8585
ctx.Context.HTML(status, string(name))
8686
}
8787

88+
// Success responses template with status 200.
89+
func (c *Context) Success(name base.TplName) {
90+
c.HTML(200, name)
91+
}
92+
8893
// RenderWithErr used for page has form validation but need to prompt error to users.
8994
func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, f interface{}) {
9095
if f != nil {

routers/user/setting.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ const (
3636
SECURITY base.TplName = "user/security"
3737
)
3838

39-
func Settings(ctx *context.Context) {
40-
ctx.Data["Title"] = ctx.Tr("settings")
41-
ctx.Data["PageIsSettingsProfile"] = true
42-
ctx.HTML(200, SETTINGS_PROFILE)
39+
func Settings(c *context.Context) {
40+
c.Data["Title"] = c.Tr("settings")
41+
c.Data["PageIsSettingsProfile"] = true
42+
c.Data["origin_name"] = c.User.Name
43+
c.Data["name"] = c.User.Name
44+
c.Data["full_name"] = c.User.FullName
45+
c.Data["email"] = c.User.Email
46+
c.Data["website"] = c.User.Website
47+
c.Data["location"] = c.User.Location
48+
c.Success(SETTINGS_PROFILE)
4349
}
4450

4551
func handleUsernameChange(ctx *context.Context, newName string) {
@@ -80,6 +86,7 @@ func handleUsernameChange(ctx *context.Context, newName string) {
8086
func SettingsPost(ctx *context.Context, f form.UpdateProfile) {
8187
ctx.Data["Title"] = ctx.Tr("settings")
8288
ctx.Data["PageIsSettingsProfile"] = true
89+
ctx.Data["origin_name"] = ctx.User.Name
8390

8491
if ctx.HasError() {
8592
ctx.HTML(200, SETTINGS_PROFILE)

templates/user/settings/profile.tmpl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@
1313
<form class="ui form" action="{{.Link}}" method="post">
1414
{{.CsrfTokenHtml}}
1515
<div class="required field {{if .Err_Name}}error{{end}}">
16-
<label for="username">{{.i18n.Tr "username"}}<span class="text red hide" id="name-change-prompt"> {{.i18n.Tr "settings.change_username_prompt"}}</span></label>
17-
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if not .SignedUser.IsLocal}}readonly{{end}}>
16+
<label for="username">{{.i18n.Tr "username"}}<span class="text red {{if eq .name .origin_name}}hide{{end}}" id="name-change-prompt"> {{.i18n.Tr "settings.change_username_prompt"}}</span></label>
17+
<input id="username" name="name" value="{{.name}}" data-name="{{.origin_name}}" autofocus required {{if not .SignedUser.IsLocal}}readonly{{end}}>
1818
{{if not .SignedUser.IsLocal}}
1919
<p class="help text blue">{{$.i18n.Tr "settings.password_username_disabled"}}</p>
2020
{{end}}
2121
</div>
2222
<div class="field {{if .Err_FullName}}error{{end}}">
2323
<label for="full_name">{{.i18n.Tr "settings.full_name"}}</label>
24-
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}">
24+
<input id="full_name" name="full_name" value="{{.full_name}}">
2525
</div>
2626
<div class="required field {{if .Err_Email}}error{{end}}">
2727
<label for="email">{{.i18n.Tr "email"}}</label>
28-
<input id="email" name="email" value="{{.SignedUser.Email}}" required>
28+
<input id="email" name="email" value="{{.email}}" required>
2929
</div>
3030
<div class="field {{if .Err_Website}}error{{end}}">
3131
<label for="website">{{.i18n.Tr "settings.website"}}</label>
32-
<input id="website" name="website" type="url" value="{{.SignedUser.Website}}">
32+
<input id="website" name="website" type="url" value="{{.website}}">
3333
</div>
3434
<div class="field">
3535
<label for="location">{{.i18n.Tr "settings.location"}}</label>
36-
<input id="location" name="location" value="{{.SignedUser.Location}}">
36+
<input id="location" name="location" value="{{.location}}">
3737
</div>
3838

3939
<div class="field">

0 commit comments

Comments
 (0)