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 Date getMinDate() 058 { 059 return _minDate; 060 } 061 062 public void validate(IFormComponent field, ValidationMessages messages, Object object) 063 throws ValidatorException 064 { 065 Date date = (Date) object; 066 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); 067 068 if (date.before(_minDate)) 069 throw new ValidatorException(buildMessage(messages, field, translator), ValidationConstraint.TOO_SMALL); 070 071 } 072 073 private String buildMessage(ValidationMessages messages, IFormComponent field, 074 DateTranslator translator) 075 { 076 return messages.formatValidationMessage( 077 getMessage(), 078 ValidationStrings.DATE_TOO_EARLY, 079 new Object[] { field.getDisplayName(), 080 (translator != null) ? 081 translator.format(field, messages.getLocale(), _minDate) : _minDate.toString()}); 082 } 083 084 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 085 FormComponentContributorContext context, IFormComponent field) 086 { 087 // TODO: This is a little hacky, but validators need to be able to cooperate 088 // with translators during client side validation as well 089 DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); 090 if (translator == null) 091 return; 092 093 JSONObject profile = context.getProfile(); 094 095 context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");"); 096 097 if (!profile.has(ValidationConstants.CONSTRAINTS)) { 098 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); 099 } 100 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); 101 102 accumulateProperty(cons, field.getClientId(), 103 new JSONLiteral("[tapestry.form.datetime.isValidDate,{" 104 + "min:" 105 + JSONObject.quote(translator.format(field, context.getLocale(), _minDate)) 106 + "," 107 + "datePattern:" 108 + JSONObject.quote(translator.getPattern()) 109 + (translator.isLenient() ? "" : ",strict:true") 110 + "}]")); 111 112 accumulateProfileProperty(field, profile, 113 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator)); 114 } 115 }