Coverage Report - org.apache.tapestry.form.validator.Required
 
Classes in this File Line Coverage Branch Coverage Complexity
Required
0%
0/21
0%
0/24
2.714
 
 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 java.util.Collection;
 18  
 
 19  
 import org.apache.tapestry.IMarkupWriter;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.form.FormComponentContributorContext;
 22  
 import org.apache.tapestry.form.IFormComponent;
 23  
 import org.apache.tapestry.form.ValidationMessages;
 24  
 import org.apache.tapestry.json.JSONObject;
 25  
 import org.apache.tapestry.multipart.UploadPart;
 26  
 import org.apache.tapestry.valid.ValidationConstants;
 27  
 import org.apache.tapestry.valid.ValidationConstraint;
 28  
 import org.apache.tapestry.valid.ValidationStrings;
 29  
 import org.apache.tapestry.valid.ValidatorException;
 30  
 
 31  
 /**
 32  
  * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  
 public class Required extends BaseValidator
 38  
 {
 39  
     public Required()
 40  0
     {
 41  0
     }
 42  
 
 43  
     public Required(String initializer)
 44  
     {
 45  0
         super(initializer);
 46  0
     }
 47  
 
 48  
     public boolean getAcceptsNull()
 49  
     {
 50  0
         return true;
 51  
     }
 52  
 
 53  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 54  
             throws ValidatorException
 55  
     {
 56  0
         if (field.isDisabled())
 57  0
             return;
 58  
         
 59  0
         if ((object == null)
 60  0
                 || (String.class.isInstance(object) && (((String) object).length() == 0))
 61  
                 || (Collection.class.isInstance(object) && ((Collection) object).isEmpty())
 62  
                 || (UploadPart.class.isInstance(object) && ((UploadPart) object).getSize() < 1))
 63  
         {
 64  0
             String message = buildMessage(messages, field);
 65  0
             throw new ValidatorException(message, ValidationConstraint.REQUIRED);
 66  
         }
 67  0
     }
 68  
     
 69  
     public String buildMessage(ValidationMessages messages, IFormComponent field)
 70  
     {
 71  0
         return messages.formatValidationMessage(
 72  
                 getMessage(),
 73  
                 ValidationStrings.REQUIRED_FIELD,
 74  
                 new Object[]
 75  
                 { field.getDisplayName() });
 76  
     }
 77  
     
 78  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 79  
             FormComponentContributorContext context, IFormComponent field)
 80  
     {
 81  0
         if(field.isDisabled())
 82  0
             return;
 83  
         
 84  0
         context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
 85  
         
 86  0
         JSONObject profile = context.getProfile();
 87  
         
 88  0
         accumulateProperty(profile, ValidationConstants.REQUIRED, field.getClientId());
 89  
         
 90  0
         accumulateProfileProperty(field, profile, 
 91  
                 ValidationConstants.REQUIRED, buildMessage(context, field));
 92  0
     }
 93  
 
 94  
     /**
 95  
      * Returns true, that's what Required means!
 96  
      */
 97  
     public boolean isRequired()
 98  
     {
 99  0
         return true;
 100  
     }
 101  
 }