Coverage Report - org.apache.tapestry5.validator.Required
 
Classes in this File Line Coverage Branch Coverage Complexity
Required
100%
10/10
100%
4/4
0
 
 1  
 // Copyright 2006, 2007, 2008 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.tapestry5.validator;
 16  
 
 17  
 import org.apache.tapestry5.Field;
 18  
 import org.apache.tapestry5.MarkupWriter;
 19  
 import org.apache.tapestry5.ValidationException;
 20  
 import org.apache.tapestry5.ioc.MessageFormatter;
 21  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 22  
 import org.apache.tapestry5.services.FormSupport;
 23  
 
 24  
 /**
 25  
  * A validator that enforces that the value is not null and not the empty string. This validator is not configurable.
 26  
  */
 27  752
 public final class Required extends AbstractValidator<Void, Object>
 28  
 {
 29  
     public Required()
 30  
     {
 31  8
         super(null, Object.class, "required");
 32  8
     }
 33  
 
 34  
     public void validate(Field field, Void constraintValue, MessageFormatter formatter, Object value)
 35  
             throws ValidationException
 36  
     {
 37  228
         if (value == null || InternalUtils.isBlank(value.toString()))
 38  32
             throw new ValidationException(buildMessage(formatter, field));
 39  196
     }
 40  
 
 41  
     private String buildMessage(MessageFormatter formatter, Field field)
 42  
     {
 43  562
         return formatter.format(field.getLabel());
 44  
     }
 45  
 
 46  
     /**
 47  
      * The exception to the rule.
 48  
      */
 49  
     public boolean isRequired()
 50  
     {
 51  694
         return true;
 52  
     }
 53  
 
 54  
     public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer,
 55  
                        FormSupport formSupport)
 56  
     {
 57  530
         formSupport.addValidation(field, "required", buildMessage(formatter, field), null);
 58  530
     }
 59  
 }