Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.form.validator;
 
 import java.util.Date;
 
Validates that the object, a Date, is not after a set maximum.

Author(s):
Howard Lewis Ship
Since:
4.0
 
 public class MaxDate extends BaseValidator
 {
     private Date _maxDate;
 
     public MaxDate()
     {
     }
 
     public MaxDate(String initializer)
     {
         super(initializer);
     }
 
     public void validate(IFormComponent fieldValidationMessages messagesObject object)
             throws ValidatorException
     {
         Date date = (Dateobject;
         DateTranslator translator = (DateTranslatorgetFieldTranslator(fieldDateTranslator.class);
         
         if (date.after())
             throw new ValidatorException(buildMessage(messagesfieldtranslator),
                     .);
         
     }
     
     private String buildMessage(ValidationMessages messagesIFormComponent field
             DateTranslator translator)
     {
         return messages.formatValidationMessage(
                 getMessage(),
                 .,
                 new Object[]
                 { field.getDisplayName(), 
                     (translator != null) ? 
                             translator.format(fieldmessages.getLocale(), )
                             : .toString()});
     }
     
     public void renderContribution(IMarkupWriter writerIRequestCycle cycle,
             FormComponentContributorContext contextIFormComponent field)
     {
         // TODO: This is a little hacky, but validators need to be able to cooperate
         // with translators during client side validation as well
         DateTranslator translator = (DateTranslatorgetFieldTranslator(fieldDateTranslator.class);
         if (translator == null)
             return;
         
         JSONObject profile = context.getProfile();
         
         context.addInitializationScript(field"dojo.require(\"tapestry.form.datetime\");");
         
         if (!profile.has(.)) {
             profile.put(.new JSONObject());
         }
         JSONObject cons = profile.getJSONObject(.);
         
         accumulateProperty(consfield.getClientId(), 
                 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
                         + "max:" 
                         + JSONObject.quote(translator.format(fieldcontext.getLocale(), ))
                         + ","
                         + "datePattern:" 
                        + JSONObject.quote(translator.getPattern())
                        + (translator.isLenient() ? "" : ",strict:true")
                        + "}]"));
        
        accumulateProfileProperty(fieldprofile
                .buildMessage(contextfieldtranslator));
    }
    
    public void setMaxDate(Date minDate)
    {
         = minDate;
    }
    public Date getMaxDate()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X