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.form.FormComponentContributorContext;
020 import org.apache.tapestry.form.IFormComponent;
021 import org.apache.tapestry.form.ValidationMessages;
022 import org.apache.tapestry.json.JSONLiteral;
023 import org.apache.tapestry.json.JSONObject;
024 import org.apache.tapestry.valid.ValidationConstants;
025 import org.apache.tapestry.valid.ValidationConstraint;
026 import org.apache.tapestry.valid.ValidationStrings;
027 import org.apache.tapestry.valid.ValidatorException;
028
029 /**
030 * Validator that ensures a string value does not exceed a maximum length.
031 *
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035 public class MaxLength extends BaseValidator
036 {
037 private int _maxLength;
038
039 public MaxLength()
040 {
041
042 }
043
044 public MaxLength(String initializer)
045 {
046 super(initializer);
047 }
048
049 public void setMaxLength(int maxLength)
050 {
051 _maxLength = maxLength;
052 }
053
054 public void validate(IFormComponent field, ValidationMessages messages, Object object)
055 throws ValidatorException
056 {
057 String string = (String) object;
058
059 if (string.length() > _maxLength)
060 throw new ValidatorException(buildMessage(messages, field),
061 ValidationConstraint.MAXIMUM_WIDTH);
062 }
063
064 protected String buildMessage(ValidationMessages messages, IFormComponent field)
065 {
066 return messages.formatValidationMessage(
067 getMessage(),
068 ValidationStrings.VALUE_TOO_LONG,
069 new Object[]
070 { new Integer(_maxLength), field.getDisplayName() });
071 }
072
073 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
074 FormComponentContributorContext context, IFormComponent field)
075 {
076 JSONObject profile = context.getProfile();
077
078 if (!profile.has(ValidationConstants.CONSTRAINTS)) {
079 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
080 }
081 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
082
083 accumulateProperty(cons, field.getClientId(),
084 new JSONLiteral("[dojo.validate.isText,{"
085 + "maxlength:" + _maxLength + "}]"));
086
087 accumulateProfileProperty(field, profile,
088 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
089 }
090
091 }