-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fix MVC form data binding localization #43182
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
Conversation
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.
Just adding a few comments to give some context in case there are any early reviewers.
Note that the way this is currently implemented, the new behavior is the default for all existing and new projects. In order to opt-out and revert to the old behavior, apps will need to do something like this: var builder = services.AddMvc()
// ...
.AddMvcOptions(options =>
{
options.SuppressCultureInvariantFormModelBinding = true;
})
.AddViewOptions(options =>
{
options.HtmlHelperOptions.SuppressCultureInvariantFormValueFormatting = true;
}); There are two options to configure because one controls the "model" end of things (parsing form values) and one controls the HTML generation (formatting form element values). We could attempt to unify these, but this would probably require breaking changes to our public API (passing an The decision to make this fix "opt-out" is not final. I'd love to hear more opinions about what the right choice is. |
How bad it is if the user sets only one of them? |
Suspect the result of setting only |
This is worth testing @MackinnonBuck. The follow-on question is basically, "Should we instead have one |
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.
Kinda just starting…
Fixed a couple issues: * Html5DateRenderingMode.CurrentCulture was not forcing CultureInfo.CurrentCulture to be used. * The "value" field in the hidden inputs did not contain the prefix (and thus did not always match the "name" field of the corresponding input).
If you only set Only setting So, AFAICT, the behavior won't get worse if you only set one of these options. Either you will get a slight improvement (generating better HTML), or no difference in behavior at all.
This is what I was hoping to do initially, but I couldn't find a good way to pass an |
So, in this case, do we really need the |
True, we might be able to go without it. I suppose the only use I can think of for it would be if you want the new behavior for generating correctly-formatted HTML EDIT: To cover the above scenario, we could possibly have one enum FormInputFormattingMode
{
// The new behavior in its entirety.
Default,
// The new form value attribute formatting, but no hidden "__Invariant" input generation.
ExcludeCultureInvariantFormRequestEntries,
// The old behavior in its entirety.
AlwaysUseCurrentCulture,
} Or we could use a |
I don't know much about |
|
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.
Some of the comments may be out of date, sorry
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.
Nothing but nits 😄
Merging now so this fix makes the cutoff - I'll open a follow-up PR that addresses the remaining nits. |
This change broke a lot of my CSS. The ':last-child' CSS selector no longer applies to the elements that have the "__Invariant" input added after them. |
Hi @bassem-mf. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context. |
What do to when you create the input fields manually - no
In model binding this gets converted to |
Fix MVC form data binding localization
This PR fixes an issue in which all form elements were being formatted and parsed using the "current" culture, while some form elements require culture-invariant formatting and parsing.
Description
Some form elements (e.g.,
<input type="text"/>
) require culture-specific formatting and parsing because their values are directly entered by the user. However, other inputs (e.g.,<input type="number"/>
) use culture-invariant formatting both in the HTML source and in the form request. Thus, we need to ensure that the correct formatting will be applied for each type of form element.The fix for this bug comes in two parts:
value
attribute when appropriate.To achieve step 2, we generate an additional HTML element for all form elements that use invariant formatting:
<input name="__Invariant" type="hidden" value="<model property name>"/>
This way, the form request gives us the information we need about which inputs need culture-invariant parsing.
Fixes #6566