voyent
calendar validation (selectinputdate)  XML
Forum Index -> Contributor's Corner
Author Message
simplysiby

Joined: 27/Apr/2010 00:00:00
Messages: 3
Offline


Hi all,
I had a requirement to provide validation to the user input in a calendar component.
Currently, in the selectinputDate component, the user is able to enter any junk data and the jsf will throw a faces exception.
But in my requirement, if the user enters a wrong date, we have to revert to the old date and show a popup to the user to enter a correct date.
Also If date entered is 2 digit,
* 01 - 49 ---- Should be changed to 21st century.
* 50 - 99 ---- Should be changed to 20th century.
And if year is 00 or 0000 it should be converted to 2000.

In the below code, what i have done is, in the faces-config.xml, i have configured

Code:
<converter>
 	<converter-for-class>java.util.Date</converter-for-class>
 	<converter-class>common.utils.DateConverter</converter-class>
 </converter>



So any change from the selectInputDate will trigger this DateConverter
i binded the converterMessage attribute of the selectInputDate with a boolean variable which is used as the rendered attribute of the popup message.
And i am setting this value as true from within the code if any wrong date is entered.

To achieve this, i used the below code.



Code:
public class DateConverter extends DateTimeConverter implements newConstants{
 	 
 	private final static Logger log = Logger.getLogger(DateConverter.class);
 	
 	public DateConverter() {
 		this.setPattern(DATE_FORMAT_MM_DD_YYYY);
 		this.setTimeZone(Calendar.getInstance().getTimeZone());
 	}
 	
 	public Object getAsObject(FacesContext facesContext, 
 		UIComponent uiComponent, 
 		String param) {
 		Date date = null;
 		if(null == param || param.trim().equals("")){
 			log.debug("Param is null or blank");
 		}else {
 			try{
 				/*
 				 * Finding if year 00 or 0000 is present and converted to 2000.
 				 * This is done to prevent converter exception in the next step
 				 */
 				String initSub = (param.substring(0,param.lastIndexOf("/")+1));
 				String sub = param.substring(param.lastIndexOf("/")+1, param.length());
 				if(null != sub && (sub.trim().equals("00") || sub.trim().equals("0000"))) {
 					param = initSub.concat("2000");
 				}
 				
 				/*
 				 * Calling getAsObject of parent to validate the input date.
 				 */
 				Date newDate = (Date)super.getAsObject(facesContext, uiComponent, param);
 
 				/*
 				 * Convert validated Date to string. 
 				 */
 				String checkStr = DateUtils.dateToString(newDate, DATE_FORMAT_MM_DD_YYYY);
 				
 				/*
 				 * Check if 2 digit year is entered in UI. Throw converter exception if some
 				 * invalid character was entered in the year column and the parser
 				 * automatically converted to a valid date. 
 				 * Eg: 20s09 will be converted to 0020. 
 				 */
 				if(null != checkStr) {
 					String validatedYear  = checkStr.substring(checkStr.lastIndexOf("/")+1, checkStr.length());
 					String paramYear  = param.substring(param.lastIndexOf("/")+1, param.length());
 					int yrLength  = paramYear.length();
 					if(yrLength != 2  && null != paramYear && 
 						!paramYear.trim().equalsIgnoreCase(validatedYear)) { 
 							throw new ConverterException();
 					}
 				}
 				
 				/*
 				 * Validate the entered year (If it falls in 2 digit or 4 digit year)
 				 */
 				if(!DateUtils.validateYear(newDate)){
 					
 					/*
 					 * If not valid, then set the old value to the component.
 					 * Set the converter message boolean to true. 
 					 * This will internally invoke the error popup to be displayed.
 					 */
 					log.debug("Date validation failed::: Reverting to old value");
 					ValueExpression valueExp = uiComponent.getValueExpression("converterMessage");
 					if(null != valueExp){
 						valueExp.setValue(facesContext.getELContext(),true);
 					}
 					date = (Date)uiComponent.getAttributes().get("value");
 				} else {
 					/*
 					 * If date is valid and is 2 digit,
 					 * 01 - 49 ---- Changed to 21st century.
 					 * 50 - 99 ---- Changed to 20th century.
 					 */
 					date = DateUtils.convertString2Date(param, DATE_FORMAT_MM_DD_YYYY);
 					date = DateUtils.convertYear(date);
 				}
 			}catch(ConverterException e)
 			{
 				/*
 				 * If date is not valid, the old value is set back to the component.
 				 * Set the converter message boolean to true. 
 				 * This will internally invoke the error popup to be displayed.
 				 */
 				log.error("Converter Exception caught::"+e);
 				ValueExpression valueExp = uiComponent.getValueExpression("converterMessage");
 				if(null != valueExp){
 					valueExp.setValue(facesContext.getELContext(),true);
 				}
 				date = (Date)uiComponent.getAttributes().get("value");
 				
 			}
 		}
 		return date;
 	}
 
 public String getAsString(FacesContext facesContext, 
 		UIComponent uiComponent, 
 		Object obj) {
 	
 	return DateUtils.dateToString((Date)obj, DATE_FORMAT_MM_DD_YYYY);
 }
 }
iceflow

Joined: 03/Nov/2011 20:59:21
Messages: 2
Offline


thanks a lot for the code

--------------------------------------------------------------
free ebook downloads free movies downloads free games downloads latest downloads my profile
 
Forum Index -> Contributor's Corner
Go to:   
Powered by JForum 2.1.7ice © JForum Team