Browse Source

user/setting: preserve user input with validation error (#1123)

pull/3696/merge
Unknwon 8 years ago
parent
commit
1afafde3b3
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 7
      modules/context/context.go
  2. 15
      routers/user/setting.go
  3. 12
      templates/user/settings/profile.tmpl

7
modules/context/context.go

@ -79,12 +79,17 @@ func (ctx *Context) HasValue(name string) bool {
return ok
}
// HTML calls Context.HTML and converts template name to string.
// HTML responses template with given status.
func (ctx *Context) HTML(status int, name base.TplName) {
log.Trace("Template: %s", name)
ctx.Context.HTML(status, string(name))
}
// Success responses template with status 200.
func (c *Context) Success(name base.TplName) {
c.HTML(200, name)
}
// RenderWithErr used for page has form validation but need to prompt error to users.
func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, f interface{}) {
if f != nil {

15
routers/user/setting.go

@ -36,10 +36,16 @@ const (
SECURITY base.TplName = "user/security"
)
func Settings(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsProfile"] = true
ctx.HTML(200, SETTINGS_PROFILE)
func Settings(c *context.Context) {
c.Data["Title"] = c.Tr("settings")
c.Data["PageIsSettingsProfile"] = true
c.Data["origin_name"] = c.User.Name
c.Data["name"] = c.User.Name
c.Data["full_name"] = c.User.FullName
c.Data["email"] = c.User.Email
c.Data["website"] = c.User.Website
c.Data["location"] = c.User.Location
c.Success(SETTINGS_PROFILE)
}
func handleUsernameChange(ctx *context.Context, newName string) {
@ -80,6 +86,7 @@ func handleUsernameChange(ctx *context.Context, newName string) {
func SettingsPost(ctx *context.Context, f form.UpdateProfile) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsProfile"] = true
ctx.Data["origin_name"] = ctx.User.Name
if ctx.HasError() {
ctx.HTML(200, SETTINGS_PROFILE)

12
templates/user/settings/profile.tmpl

@ -13,27 +13,27 @@
<form class="ui form" action="{{.Link}}" method="post">
{{.CsrfTokenHtml}}
<div class="required field {{if .Err_Name}}error{{end}}">
<label for="username">{{.i18n.Tr "username"}}<span class="text red hide" id="name-change-prompt"> {{.i18n.Tr "settings.change_username_prompt"}}</span></label>
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if not .SignedUser.IsLocal}}readonly{{end}}>
<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>
<input id="username" name="name" value="{{.name}}" data-name="{{.origin_name}}" autofocus required {{if not .SignedUser.IsLocal}}readonly{{end}}>
{{if not .SignedUser.IsLocal}}
<p class="help text blue">{{$.i18n.Tr "settings.password_username_disabled"}}</p>
{{end}}
</div>
<div class="field {{if .Err_FullName}}error{{end}}">
<label for="full_name">{{.i18n.Tr "settings.full_name"}}</label>
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}">
<input id="full_name" name="full_name" value="{{.full_name}}">
</div>
<div class="required field {{if .Err_Email}}error{{end}}">
<label for="email">{{.i18n.Tr "email"}}</label>
<input id="email" name="email" value="{{.SignedUser.Email}}" required>
<input id="email" name="email" value="{{.email}}" required>
</div>
<div class="field {{if .Err_Website}}error{{end}}">
<label for="website">{{.i18n.Tr "settings.website"}}</label>
<input id="website" name="website" type="url" value="{{.SignedUser.Website}}">
<input id="website" name="website" type="url" value="{{.website}}">
</div>
<div class="field">
<label for="location">{{.i18n.Tr "settings.location"}}</label>
<input id="location" name="location" value="{{.SignedUser.Location}}">
<input id="location" name="location" value="{{.location}}">
</div>
<div class="field">

Loading…
Cancel
Save