0
<xp:validateExpression
                            message="İzinler pazar günü başlayamaz.">
                            <xp:this.expression><![CDATA[#{javascript:var DateConverter = {
 dateToString: function( date:java.util.Date, pattern:String ){
  try {
   if( !date ){ return ''; }

   var formatter = DateConverter.getFormatter( pattern );
   return formatter.format( date );
  } catch( e ){
   // ErrorHandling
  }
 },

 stringToDate: function( dateString:String, pattern:String ){
  try {
   if( !dateString ){ return null; }

   var formatter = DateConverter.getFormatter( pattern );
   return formatter.parse( dateString );
  } catch( e ){
   // ErrorHandling
  }
 },

 getFormatter: function( pattern:String ){
  try {
   var cacheKey = 'dateFormatter' + pattern;
   var dateFormatter = applicationScope[ cacheKey ];
   if( !dateFormatter ){
    dateFormatter = new java.text.SimpleDateFormat( pattern );
    applicationScope[ cacheKey ] = dateFormatter;
   }

   return dateFormatter;
  } catch( e ){
   // ErrorHandling
  }
 } 
};

vDate = DateConverter.stringToDate(getComponent("LeaveDate").getSubmittedValue(), 'dd.MM.yyyy' );
return (@Weekday(vDate) != 1);
}]]></xp:this.expression>

I'm trying to add a validation to restrict the selection of sundays. But expression returns True regardless of date I select.

The snippet above is to convert string to date, because I read that getSubmittedValue() returns string regardless of the source field type.

return (@Weekday(getComponent("LeaveDate").getSubmittedValue()) != 1);

My first code was this but I couldn't get it work either.

senbon
  • 766
  • 5
  • 12

2 Answers2

1

It may be worth looking at Java Calendar class. To load a date into it you use:

java.util.Calendar.getInstance();
java.util.Calendar.setTime(date);

You can then use java.util.Calendar.get(java.util.Calendar.DAY_OF_WEEK) and check it against the static field java.util.Calendar.SUNDAY

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • I guess my real problem is that i can't get a value with date type. It returns as string. Maybe if I try very hard to create a date from string, i can succeed but I reckon there should be a cleaner way. And if it's no big hassle can you elaborate the code you've suggested? I'm not very well versed in java but I'm trying to improve. – senbon May 20 '14 at 09:54
  • The fact it's a String is not a problem and completely normal for Validators. Process Validation phase takes the String input from the browser (remember everything in a browser is actually a String, because HTML is text). It then passes it through Converters (to make sure the String is valid in terms of being able to converted to the underlying datatype. It then passes it through Validators to perform any business-related validation, like you have here. So what you can just do is use a try/catch to create a date from the string, and if it goes into the catch, you know it's not a valid date. – Paul Stephen Withers May 20 '14 at 12:10
  • This should give you some sample code http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date – Paul Stephen Withers May 20 '14 at 12:15
  • function ynTextToDate(v) { try { if (typeof v != "string") return v; var vd = @Explode(v , "."); var ty = vd[2].slice(0,4); var td = vd[0]; var tm = vd[1]; return @Date(ty,tm,td); //return td+"." + tm + "." + ty } catch (e) { print("ynTextToDate "+e); } } I've created a fuction to convert string to date. It works successfully on a computed field and returns the day of week. But validation turns True no matter what. I can't see what .getSubmittedValue() returns so I have no idea what the problem is. – senbon May 20 '14 at 15:54
  • I think the article I wrote on JSF lifecycle will be helpful for understanding what's happening when you're running the validator http://www.intec.co.uk/understanding-partial-execution-part-three-jsf-lifecycle/. The SSJS debugger is worth using, if you're on R9, otherwise you'll have to resort to print statements. Chapter 4 of Mastering XPages 2nd Edition has in depth examples of Validators and returning errors. I'm afraid I didn't code my own validators until I was doing Java, so I'm not sure of where it's going wrong. – Paul Stephen Withers May 21 '14 at 10:06
0

In a validation formula the value to be validated is available as value. So your validation formula needs to be simpler:

      @Weekday(value) !== 1;

Does that work for you?

stwissel
  • 20,110
  • 6
  • 54
  • 101
  • If you look at the last line of my code you will see that i used the same. But getting value part is the problem. – senbon May 20 '14 at 09:39
  • No you didn't. You tried to use getSubmittedValue() where you actually really only need to type v a l u e. - value is not a placeholder here but the actual name of the variable containing the submitted value for the control where the validation formula is. So you really type: value (all small) – stwissel May 20 '14 at 23:05