vaadin / framework

Vaadin 6, 7, 8 is a Java framework for modern Java web applications.
http://vaadin.com/
Other
1.78k stars 729 forks source link

Bean Validation uses (wrong) default locale #4063

Open vaadin-bot opened 11 years ago

vaadin-bot commented 11 years ago

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

vaadin-bot commented 11 years ago

Originally by DimitriL


Attachment added: Validation.gif (9.6 KiB) Validation.gif https://trac-attachments.vaadin.com/trac/12016/Validation.gif

vaadin-bot commented 11 years ago

Originally by DimitriL


Attachment added: Validation.zip (12.7 KiB)

vaadin-bot commented 11 years ago

Originally by @hesara


For a workaround see https://vaadin.com/old-forum/-/message_boards/view_message/3366235 .

Getting the correct locale for validation on the framework is somewhat more complicated, though - maybe it should come from each field to its validators so that setting the locale for a field or a layout would keep consistency between validators and fields.

vaadin-bot commented 11 years ago

Originally by oehme


I think this should have a priority, as the "workaround" is a mess and only works on commit. BeanValidation is basically useless without the correct locale.

Validators should probably have a reference to their field, so that they can use "getLocale" at validation time.

vaadin-bot commented 11 years ago

Originally by DimitriL


I think it would be even ok to take the locale from the UI. UI.getCurrent()...

stale[bot] commented 6 years ago

Hello there!

It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!