-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
(Preview) Fix no same naming convention between template and form #22846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Preview) Fix no same naming convention between template and form #22846
Conversation
modules/web/middleware/binding.go
Outdated
@@ -42,7 +42,7 @@ func AssignForm(form interface{}, data map[string]interface{}) { | |||
if fieldName == "-" { | |||
continue | |||
} else if len(fieldName) == 0 { | |||
fieldName = util.ToSnakeCase(field.Name) | |||
fieldName = field.Name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the snake_case is done by purpose:
- snake_case: for auto filled form, matching the form element's
name
- PascalCase: for non-form variables
?
If the form-variables and non-form-variables are mixed together, it would be very difficult to understand what a variable is used for, and may cause uncontrollable overwriting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wxiaoguang
Thanks for your review.
You are right, it is difficult to distinguish and may cause uncontrollable overwriting.
How about the following changes.
For non-form variables:
// in func
ctx.Data["HelloData"]
// in template
{{.HelloData}}
For auto filled form:
// in func
ctx.Data["Form"] = forms.FormStruct {
Variable: vaule,
// ....
}
// in template
{{.Form.Variable}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure and I haven't thought about it carefully.
Maybe the Technical Oversight Committee could help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I like the idea {{.Form.Variable}}
, it's more readable and go-style. And there will be a clear way to check if it's an error page to repost with entered form: {{if .Form}} /* keep the values in .Form */ {{end}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the point of the snake_case but using the {{.Form}}
would prevent us from having to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there is one potential issue with the .Form
object if you're using go code.
I'm not certain if there's any code that does this - but I could imagine some code that doesn't have the type of the Form object but would have been able to update the values in the ctx.Data["some_field"]
which by convention was a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some pages, there are several forms in one page. Need to consider a new solution to solve this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some pages, there are several forms in one page. Need to consider a new solution to solve this problem.
a64ba42 is an example to fix this problem.
Since this is a big decision, I think I should call the TOC. |
This is a cautious 👍 from me. The My caution is because when you put stuff in I think there are a few places where we reuse the same or similar templates with different forms but I'm not sure if we have go code that would adjust/use the snake_cases fields. If we do that - then the As an aside... in order to make migration safer and easier I would recommend leaving in the current auto snake_case mapping but also just stick the Form in {{.Form}} and leave a note to remove it soon. |
An advantage of the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well… Everything seems to work.
Although I haven't understood yet how the other contents are auto-injected, when all you do is swap out the maps you use…
@delvh |
Codecov Report
@@ Coverage Diff @@
## main #22846 +/- ##
==========================================
- Coverage 47.14% 47.00% -0.15%
==========================================
Files 1149 1158 +9
Lines 151446 153166 +1720
==========================================
+ Hits 71397 71989 +592
- Misses 71611 72676 +1065
- Partials 8438 8501 +63
... and 70 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
I have some clearer ideas about this problem now. The root problem is that it's very difficult to re-fill the forms with correct values, the case is:
There is no clear / general approach for various cases/pages. I am pretty sure because I have made various different web frameworks by Java/PHP/Go, the "form-post-refill" problem has no clear solution. And even worse, there are a lot of misuses in old form-binding code (aka Then, how to make the UI/UX friendly to end users if something wrong happens? I think the answer is AJAX.
And the AJAX Form can handle more cases, eg: internal server error, broken network, etc. |
If we use AJAX, the change will be much bigger. |
IMO it's not that big, the most work is rewriting the |
Is it possible to reuse API code? For example: Or |
No, the API handlers and Web handlers shouldn't be mixed together. API handlers focuses on stability, they do not change too much. Web handlers focuses on flexibility, and are only used by Web UI (so no swagger document)
Neither. The AJAX forms could just post to the original web page "post" handlers. This could be a simple change: from |
Now there is a framework level support for making AJAX requests. Here is a sample for how to do it: Use fetch to send requests to create issues/comments #25258 Only 2 steps:
That's all. I think this issue can be closed. Move more forms to the AJAX world. |
Fixes #22833
(This PR is still on working)
The variable's names in form data will be changed to snakecase in func
AssignForm
ifform
tag is not defined.And this makes contributors write various code which follows different naming convention of variables.
Then low readability code caused the bug I mentioned in #22833
(I personally think so)
So I made a change to
AssignForm
:The variable's names in form data will be changed into field's name.
Then we just let the variable's names in context data as same as the field's name which is simply defined in each form struct.
(easy to check whether the variable's name is currect, otherwise the vaule of therm will be changed when
RenderWithErr
or some unknow bugs will be occured?)This is a big change, because we need to check all functions uses
AssignForm
.So I made this preview PR, if it is worth to spend time on this work, I will continue to work on this.