11package org .fugerit .java .core .validator ;
22
33import java .text .ParseException ;
4+ import java .text .ParsePosition ;
45import java .text .SimpleDateFormat ;
56import java .util .Date ;
67import java .util .Properties ;
78
89import org .fugerit .java .core .cfg .ConfigException ;
10+ import org .fugerit .java .core .lang .helpers .BooleanUtils ;
911import org .fugerit .java .core .lang .helpers .StringUtils ;
1012
1113public class ValidatorDate extends BasicValidator {
@@ -20,7 +22,9 @@ public class ValidatorDate extends BasicValidator {
2022 public static final String KEY_MINDATE = "minDate" ;
2123
2224 public static final String KEY_MAXDATE = "maxDate" ;
23-
25+
26+ public static final String KEY_STRICT = "strict" ;
27+
2428 public static final String ERROR_KEY_DATE = "error.date" ;
2529
2630 public static final String ERROR_KEY_DATE_MIN = "error.date.min" ;
@@ -32,7 +36,13 @@ public class ValidatorDate extends BasicValidator {
3236 private String minDate ;
3337
3438 private String maxDate ;
35-
39+
40+ private boolean strict ;
41+
42+ public ValidatorDate () {
43+ this .strict = Boolean .FALSE ;
44+ }
45+
3646 public String getDateFormat () {
3747 return dateFormat ;
3848 }
@@ -45,6 +55,10 @@ public String getMaxDate() {
4555 return maxDate ;
4656 }
4757
58+ public boolean isStrict () {
59+ return this .strict ;
60+ }
61+
4862 protected Date setDate ( SimpleDateFormat sdf , String d ) throws ParseException {
4963 Date res = null ;
5064 if ( StringUtils .isNotEmpty ( d ) ) {
@@ -77,6 +91,10 @@ public void configure( Properties atts ) throws ConfigException {
7791 if ( StringUtils .isNotEmpty ( maxDateLocal ) ) {
7892 this .maxDate = maxDateLocal ;
7993 }
94+ String strictLocal = atts .getProperty ( KEY_STRICT );
95+ if ( StringUtils .isNotEmpty ( strictLocal ) ) {
96+ this .strict = BooleanUtils .isTrue ( strictLocal );
97+ }
8098 } catch (Exception e ) {
8199 throw new ConfigException ( e );
82100 }
@@ -93,7 +111,9 @@ protected boolean validate( ValidatorContext context, String minDate, String max
93111 boolean valid = true ;
94112 try {
95113 SimpleDateFormat sdf = new SimpleDateFormat ( this .getDateFormat () );
96- Date d = sdf .parse ( context .getValue () );
114+ sdf .setLenient ( !this .isStrict () );
115+ ParsePosition pp = new ParsePosition ( 0 );
116+ Date d = sdf .parse ( context .getValue (), pp );
97117 if ( StringUtils .isNotEmpty ( minDate ) && d .before ( sdf .parse ( minDate ) ) ) {
98118 valid = false ;
99119 String message = this .formatMessage ( context .getBundle () , ERROR_KEY_DATE_MIN , context .getLabel (), context .getValue (), minDate );
@@ -104,6 +124,9 @@ protected boolean validate( ValidatorContext context, String minDate, String max
104124 String message = this .formatMessage ( context .getBundle () , ERROR_KEY_DATE_MAX , context .getLabel (), context .getValue (), maxDate );
105125 context .getResult ().addError ( context .getFieldId (), message );
106126 }
127+ if ( this .isStrict () && pp .getIndex () != context .getValue ().length () ) {
128+ throw new ParseException ( context .getValue (), pp .getIndex () );
129+ }
107130 } catch (Exception e ) {
108131 valid = false ;
109132 String message = this .formatMessage ( context .getBundle () , ERROR_KEY_DATE , context .getLabel (), context .getValue (), StringUtils .valueWithDefault ( this .getInfo (), this .getDateFormat () ) );
0 commit comments