001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.validator;
016
017 import java.util.Date;
018
019 import org.apache.tapestry.IMarkupWriter;
020 import org.apache.tapestry.IRequestCycle;
021 import org.apache.tapestry.form.FormComponentContributorContext;
022 import org.apache.tapestry.form.IFormComponent;
023 import org.apache.tapestry.form.ValidationMessages;
024 import org.apache.tapestry.form.translator.DateTranslator;
025 import org.apache.tapestry.json.JSONLiteral;
026 import org.apache.tapestry.json.JSONObject;
027 import org.apache.tapestry.valid.ValidationConstants;
028 import org.apache.tapestry.valid.ValidationConstraint;
029 import org.apache.tapestry.valid.ValidationStrings;
030 import org.apache.tapestry.valid.ValidatorException;
031
032 /**
033 * Validates that the object, a Date, is not after a set maximum.
034 *
035 * @author Howard Lewis Ship
036 * @since 4.0
037 */
038 public class MaxDate extends BaseValidator
039 {
040 private Date _maxDate;
041
042 public MaxDate()
043 {
044 }
045
046 public MaxDate(String initializer)
047 {
048 super(initializer);
049 }
050
051 public void validate(IFormComponent field, ValidationMessages messages, Object object)
052 throws ValidatorException
053 {
054 Date date = (Date) object;
055 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
056
057 if (date.after(_maxDate))
058 throw new ValidatorException(buildMessage(messages, field, translator),
059 ValidationConstraint.TOO_LARGE);
060
061 }
062
063 private String buildMessage(ValidationMessages messages, IFormComponent field,
064 DateTranslator translator)
065 {
066 return messages.formatValidationMessage(
067 getMessage(),
068 ValidationStrings.DATE_TOO_LATE,
069 new Object[]
070 { field.getDisplayName(),
071 (translator != null) ?
072 translator.format(field, messages.getLocale(), _maxDate)
073 : _maxDate.toString()});
074 }
075
076 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
077 FormComponentContributorContext context, IFormComponent field)
078 {
079 // TODO: This is a little hacky, but validators need to be able to cooperate
080 // with translators during client side validation as well
081 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
082 if (translator == null)
083 return;
084
085 JSONObject profile = context.getProfile();
086
087 context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
088
089 if (!profile.has(ValidationConstants.CONSTRAINTS)) {
090 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
091 }
092 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
093
094 accumulateProperty(cons, field.getClientId(),
095 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
096 + "max:"
097 + JSONObject.quote(translator.format(field, context.getLocale(), _maxDate))
098 + ","
099 + "datePattern:"
100 + JSONObject.quote(translator.getPattern())
101 + (translator.isLenient() ? "" : ",strict:true")
102 + "}]"));
103
104 accumulateProfileProperty(field, profile,
105 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
106 }
107
108 public void setMaxDate(Date minDate)
109 {
110 _maxDate = minDate;
111 }
112
113 public Date getMaxDate()
114 {
115 return _maxDate;
116 }
117 }