Help on Form Validation!

Hello,

I have searched the forum for hours without success, please help me.
I got a form in which one of the TextFiled registered a StringLengthValidator.
If I call commit(), isValid() or validate() on the form, it will throw InvalidValueException follow by a notification to the client side. The textfiled also has proper error message indicating the textfield has failed the validation, this is correct.
The problem is that this InvalidValueException will not be caught on the button listener. How can I catch this exception and disable the notification?

Here is my partial code:


@Configurable
public class JobEditWindow extends Window 
{
	private static final long serialVersionUID = -6596707217856351847L;
	private VerticalLayout vl1;
	private HorizontalLayout hl1;
	private Button saveBtn;
	private ViewBoundForm form;
	@Resource private ResumeDAO dao;

	public JobEditWindow() {
		super();
	}

	public JobEditWindow(String caption, ComponentContainer content) {
		super(caption, content);
	}

	public JobEditWindow(String caption) {
		super(caption);
	}
	
	@PostConstruct
	public void init()
	{
		setWidth("550px");
		vl1 = new VerticalLayout();
		vl1.setMargin(true);
		form = new ViewBoundForm(new JobFormComp());
		form.setImmediate(false);
		form.setWriteThrough(false);
		form.setInvalidCommitted(false);
		vl1.addComponent(form);
		saveBtn = new Button("SAVE");
		hl1 = new HorizontalLayout();
		hl1.addComponent(saveBtn);
		vl1.addComponent(hl1);
		setContent(vl1);
		
		saveBtn.addListener(new ClickListener()
		{
			private static final long serialVersionUID = 6225427029801552571L;
			@Override
			public void buttonClick(ClickEvent event) 
			{
				try
				{
					form.commit();  //same as form.isValid()  or form.validate()
				}
				catch(Exception e)
				{
					System.out.print("ERROR");  //this line can not be reached.
				}
			
			}
		});
	}

	/**
	 * @return the form
	 */
	public ViewBoundForm getForm() {
		return form;
	}
}

I do something similar all the time and it works fine for me:

                    try {
                        form.validate();
                    } catch (InvalidValueException ive) {
                        // get root cause of ive and show notification
                        return;
                    }

How do you know it’s not getting into your catch block? If through a debugger, then you ought to be able to step into the commit() method to see what’s going on. Since Vaadin is open-source, grabbing the sources to step through is a huge help to understanding it. If you’re not trying this is a debugger, then make sure your output actually goes somewhere. For instance, add a println statement before and after the “form.commit()” call.

If you can’t figure it out, try creating a simpler test case (e.g. one Application class with inner classes for listeners) and post that so that someone can try the whole thing. Often, the act of creating the simple test case helps you see the problem if there is one.

Note that InvalidValueExceptions should be handled differently than other exceptions since it has an array of causes instead of a single Throwable cause like other exceptions. Maybe that’s already obvious to everyone else though. :slight_smile:

Cheers,
Bobby

I have exactly the same problem