Vaadin 7 bean validation message language (locale)

Dear All,

I’ve already tried to post it in the Vaadin 7 section, but didn’t get any answer, so I will try here in the hope someone will come up with a suggestion.

I am prototyping with the bean validation and I am unable to show messages in proper languages. Instead always the default locale is taken.

I debugged it and 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.

Is there any suggestion how can I plugin into the validation process to overcome the issue?

Here is my test code:


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);
	}
}

In case somebody is still interested in the resolution.

Here is the workaround code till the issue is fixed in the frame work:

	
   /**
	 * Commits all changes done to the bound fields.
	 * 
	 * See {@link BeanFieldGroup#commit()} 
	 */
	public void commit() {
		try {
			this.setValidationLocale(); //Workaround, set locale before commit.
			
			this.fieldGroup.commit();
		}
		catch (CommitException exception) {
			throw new RuntimeException(exception.toString(), exception); //TODO: proper handling
		}
	} 
	
	/**
	 * Workaround for the vaadin bug http://dev.vaadin.com/ticket/12016.
	 * 
	 * We set the locale manually for all {@link BeanValidator} validators.
	 */
	private void setValidationLocale() {
		Collection<Field<?>> fields = this.fieldGroup.getFields();
		
		for (Field<?> field : fields) {
			Collection<Validator> validators = field.getValidators();
			for (Validator validator : validators) {
				if (validator instanceof BeanValidator) {
					//ResourceManager => Own locale handling class. super.getLocale() would also work for CustomComponents 
					((BeanValidator)validator).setLocale(ResourceManager.getCurrentLocale()); 
				}
			}
		}
	}

Hi

I have the same problem. The validation messages need to be localised i.e. english, german etc.
I can’t quite see how to use the workaround above, I use BeanItem, FieldGroup and a FormLayout and simply use:

myFieldGroup.bindMemberFields(myFormLayout); The validation happens in the framework. Where (which class/object) would I add the workaround to?

I see the ticket (http://dev.vaadin.com/ticket/12016) is still there, although the original post linked to in the ticket in the old forum is no longer available. Is the ticket likely to be fixed any time soon?

Thanks for any help or advice.

David

Hi,

I have the same problem. The workaround is not a practical solution. To my mind the problem didn’t exists in Vaadin 6.
Do you know, if there is a update regarding this issue?!

Thanks,
Volker

One more year later I face the same problem. Still no solution available?