Bean Validation uses (wrong) default locale
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.8k
- Forks
- 717
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 3
Description
Originally by DimitriL
Given is the code (also available in the attachment as a project):
First appearance of the fields shows the validation errors in the German language (see the screen shot) but the locale is set to UK. The reason is in the method of the AbstractComponent#getLocale() which returns null as the locale because the field is not yet attached to the parent, but the validator is already initialized with the default (wrong) locale.
public class ValidationUI extends UI {
@Override
protected void init(VaadinRequest request) {
VaadinSession.getCurrent().setLocale(Locale.UK);
setLocale(Locale.UK);
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Project project = new Project();
final TextField textField = new TextField("Name");
textField.setNullRepresentation("");
textField.setWidth("150px");
final BeanFieldGroup<Project> fieldGroup = new BeanFieldGroup<Project>(Project.class);
fieldGroup.setItemDataSource(project);
fieldGroup.bind(textField, "name");
FormLayout formLayout = new FormLayout(textField);
formLayout.addComponent(textField);
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
try {
layout.addComponent(new Label("Field Locale: " + textField.getLocale()));
fieldGroup.commit();
} catch (CommitException e) {
throw new RuntimeException(e);
}
}
});
layout.addComponent(formLayout);
layout.addComponent(button);
}
}
Imported from https://dev.vaadin.com/ issue #12016
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior with the ValidationUI example and inspect AbstractComponent#getLocale together with BeanFieldGroup initialization. Trace when the field is attached relative to validator setup; done means the first validation messages use the UI or session Locale.UK rather than the system default.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- internationalization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100