How to invoke datefield error without throwing exception?

Hi,

I have two datefields: From date and To date. Besides doing default validations (e.g., format, value etc) I also need to check whether From date is less than To date or not (which should be checked in a valueChangeEvent). And if erroneous input is given I want the datefield’s .v-datefield-error css to be triggered. The normal validation part works fine, which I have achieved with the following code:

 toDate = new DateField( "To: " ){
			@Override
		protected Date handleUnparsableDateString(String dateString) throws com.vaadin.data.Property.ConversionException {
				try{
		    		Date date = dateValidator(dateString, "to");
		    		return date;
			        
		    	}catch(Exception e){
		    		// Have a notification for the error
		    		getWindow().showNotification(
		    				e.getMessage(),
			                Notification.TYPE_WARNING_MESSAGE);
			        
			        // A failure must always also throw an exception
			        throw new Property.ConversionException("Invalid date");
		    	}
			}
			};


private Date dateValidator(String dateTime, String dateFieldType) throws Exception{
		
		String regex1 = "^(19|20)\\d\\d[-/.]
(0[1-9]
|1[012]
)[-/.]
(0[1-9]
|[12]
[0-9]
|3[01]
)$";
		String regex2 = "^(19|20)\\d\\d[-/.]
(0[1-9]
|1[012]
)[-/.]
(0[1-9]
|[12]
[0-9]
|3[01]
)\\s[0-2]
[0-3]
:[0-5]
[0-9]
:[0-5]
[0-9]
$";
		
		if(dateTime.matches(regex1)){
		dateTime = dateTime.replaceAll("[-/.]
", "-");
		try{
			Date date = null;
		if(dateFieldType.equalsIgnoreCase("from")){
			 date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(dateTime + " 00:00:00");
				
		}
		else if(dateFieldType.equalsIgnoreCase("to")){
			 date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(dateTime + " 23:59:59");
			
		}
		return date;
		}catch (Exception e) {
			throw new Exception("Invalid Date");
		}
		
		}
		else if(dateTime.matches(regex2)){
			dateTime = dateTime.replaceAll("[-/.]
", "-");
			try{
				Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(dateTime);
				return date;
				}catch (Exception e) {
					throw new Exception("Invalid Date");
				}
		}
		else throw new Exception("Invalid Date");
		
		
	}

But, the part when the datefield’s value changes and I am supposed to check whether From date is less than To date, throws exception (that I am throwing in order to invoke the datefield’s .v-datefield-error css to be triggered.)

 toDate.addListener( new ValueChangeListener( ) {
			
			@Override
			public void valueChange( ValueChangeEvent event ) {
				
				if(toDate.getValue( )!=null && fromDate.getValue( )!=null && ((Date)fromDate.getValue( )).compareTo( (Date)toDate.getValue( ) ) > 0)
				{
					window.showNotification( "From date must be less than To date", Notification.TYPE_WARNING_MESSAGE );
					throw new Property.ConversionException("From date must be less than To date");
				}
				else {
					expiryDateTo.setComponentError(null);
				}

			}
		} );

Following is the stackt race.


com.vaadin.event.ListenerMethod$MethodException: Invocation of method valueChange in au.com.touchnetworks.vas.inventory.web.SearchImports$4 failed.
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:530)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)
	at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:937)
	at com.vaadin.ui.AbstractField.setValue(AbstractField.java:569)
	at com.vaadin.ui.DateField.setValue(DateField.java:529)
	at com.vaadin.ui.DateField.changeVariables(DateField.java:437)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1460)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1404)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1329)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:761)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1770)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)
Caused by: com.vaadin.data.Property$ConversionException: From date must be less than To date
	at au.com.touchnetworks.vas.inventory.web.SearchImports$4.valueChange(SearchImports.java:271)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
	... 29 more

How can I get rid of it.? If I catch it then the purpose is not served. The other validations do not show this huge stack trace! Just the message I have set. Please suggest me a solution.

I found a fix! Instead of throwing exception I am now doing the following:

 toDate.setComponentError(new UserError("From date must be less than To date"));

And my problem is solved!!! :slight_smile: