Coverage Report - org.apache.tapestry.form.validator.MinLength
 
Classes in this File Line Coverage Branch Coverage Complexity
MinLength
0%
0/19
0%
0/4
1.429
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form.validator;
 16  
 
 17  
 import org.apache.tapestry.IMarkupWriter;
 18  
 import org.apache.tapestry.IRequestCycle;
 19  
 import org.apache.tapestry.form.FormComponentContributorContext;
 20  
 import org.apache.tapestry.form.IFormComponent;
 21  
 import org.apache.tapestry.form.ValidationMessages;
 22  
 import org.apache.tapestry.json.JSONLiteral;
 23  
 import org.apache.tapestry.json.JSONObject;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 import org.apache.tapestry.valid.ValidationConstraint;
 26  
 import org.apache.tapestry.valid.ValidationStrings;
 27  
 import org.apache.tapestry.valid.ValidatorException;
 28  
 
 29  
 /**
 30  
  * Validates that the value, a string, is of a minimum length.
 31  
  * 
 32  
  * @author Howard Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  
 public class MinLength extends BaseValidator
 36  
 {
 37  
     private int _minLength;
 38  
 
 39  
     public MinLength()
 40  0
     {
 41  0
     }
 42  
 
 43  
     public MinLength(String initializer)
 44  
     {
 45  0
         super(initializer);
 46  0
     }
 47  
 
 48  
     public void setMinLength(int minLength)
 49  
     {
 50  0
         _minLength = minLength;
 51  0
     }
 52  
 
 53  
     public int getMinLength()
 54  
     {
 55  0
         return _minLength;
 56  
     }
 57  
 
 58  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 59  
             throws ValidatorException
 60  
     {
 61  0
         String string = (String) object;
 62  
 
 63  0
         if (string.length() < _minLength)
 64  0
             throw new ValidatorException(buildMessage(messages, field),
 65  
                     ValidationConstraint.MINIMUM_WIDTH);
 66  0
     }
 67  
 
 68  
     protected String buildMessage(ValidationMessages messages, IFormComponent field)
 69  
     {
 70  0
         return messages.formatValidationMessage(
 71  
                 getMessage(),
 72  
                 ValidationStrings.VALUE_TOO_SHORT,
 73  
                 new Object[]
 74  
                 { new Integer(_minLength), field.getDisplayName() });
 75  
     }
 76  
 
 77  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 78  
             FormComponentContributorContext context, IFormComponent field)
 79  
     {
 80  0
         JSONObject profile = context.getProfile();
 81  
         
 82  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 83  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 84  
         }
 85  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 86  
         
 87  0
         accumulateProperty(cons, field.getClientId(), 
 88  
                 new JSONLiteral("[tapestry.form.validation.isText,{"
 89  
                         + "minlength:" + _minLength + "}]"));
 90  
         
 91  0
         accumulateProfileProperty(field, profile, 
 92  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 93  0
     }
 94  
 }