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 org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.TapestryUtils;
020    import org.apache.tapestry.form.FormComponentContributorContext;
021    import org.apache.tapestry.form.IFormComponent;
022    import org.apache.tapestry.form.ValidationMessages;
023    import org.apache.tapestry.valid.ValidationConstraint;
024    import org.apache.tapestry.valid.ValidationStrings;
025    import org.apache.tapestry.valid.ValidatorException;
026    
027    /**
028     * Validator that ensures a string value does not exceed a maximum length.
029     * 
030     * @author Howard Lewis Ship
031     * @since 4.0
032     */
033    public class MaxLength extends BaseValidator
034    {
035        private int _maxLength;
036    
037        public MaxLength()
038        {
039    
040        }
041    
042        public MaxLength(String initializer)
043        {
044            super(initializer);
045        }
046    
047        public void setMaxLength(int maxLength)
048        {
049            _maxLength = maxLength;
050        }
051    
052        public void validate(IFormComponent field, ValidationMessages messages, Object object)
053                throws ValidatorException
054        {
055            String string = (String) object;
056    
057            if (string.length() > _maxLength)
058                throw new ValidatorException(buildMessage(messages, field),
059                        ValidationConstraint.MAXIMUM_WIDTH);
060        }
061    
062        protected String buildMessage(ValidationMessages messages, IFormComponent field)
063        {
064            return messages.formatValidationMessage(
065                    getMessage(),
066                    ValidationStrings.VALUE_TOO_LONG,
067                    new Object[]
068                    { new Integer(_maxLength), field.getDisplayName() });
069        }
070    
071        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
072                FormComponentContributorContext context, IFormComponent field)
073        {
074            context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
075    
076            StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_max_length(event, '");
077            buffer.append(field.getClientId());
078            buffer.append("', ");
079            buffer.append(_maxLength);
080            buffer.append(", ");
081            buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
082            buffer.append("); }");
083    
084            context.addSubmitHandler(buffer.toString());
085        }
086    
087    }