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 * Expects the value to be a {@link Date}, and constrains the date to follow a particular date.
034 *
035 * @author Howard M. Lewis Ship
036 * @since 4.0
037 */
038
039 public class MinDate extends BaseValidator
040 {
041 private Date _minDate;
042
043 public MinDate()
044 {
045 }
046
047 public MinDate(String initializer)
048 {
049 super(initializer);
050 }
051
052 public void setMinDate(Date minDate)
053 {
054 _minDate = minDate;
055 }
056
057 public void validate(IFormComponent field, ValidationMessages messages, Object object)
058 throws ValidatorException
059 {
060 Date date = (Date) object;
061 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
062
063 if (date.before(_minDate))
064 throw new ValidatorException(buildMessage(messages, field, translator),
065 ValidationConstraint.TOO_SMALL);
066
067 }
068
069 private String buildMessage(ValidationMessages messages, IFormComponent field,
070 DateTranslator translator)
071 {
072 return messages.formatValidationMessage(
073 getMessage(),
074 ValidationStrings.DATE_TOO_EARLY,
075 new Object[]
076 { field.getDisplayName(),
077 (translator != null) ?
078 translator.format(field, messages.getLocale(), _minDate)
079 : _minDate});
080 }
081
082 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
083 FormComponentContributorContext context, IFormComponent field)
084 {
085 // TODO: This is a little hacky, but validators need to be able to cooperate
086 // with translators during client side validation as well
087 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
088 if (translator == null)
089 return;
090
091 JSONObject profile = context.getProfile();
092
093 context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
094
095 if (!profile.has(ValidationConstants.CONSTRAINTS)) {
096 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
097 }
098 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
099
100 accumulateProperty(cons, field.getClientId(),
101 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
102 + "min:"
103 + JSONObject.quote(translator.format(field, context.getLocale(), _minDate))
104 + ","
105 + "datePattern:"
106 + JSONObject.quote(translator.getPattern())
107 + "}]"));
108
109 accumulateProfileProperty(field, profile,
110 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
111 }
112 }