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 * Validates that the value, a string, is of a minimum length.
031 *
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035 public class MinLength extends BaseValidator
036 {
037 private int _minLength;
038
039 public MinLength()
040 {
041 }
042
043 public MinLength(String initializer)
044 {
045 super(initializer);
046 }
047
048 public void setMinLength(int minLength)
049 {
050 _minLength = minLength;
051 }
052
053 public int getMinLength()
054 {
055 return _minLength;
056 }
057
058 public void validate(IFormComponent field, ValidationMessages messages, Object object)
059 throws ValidatorException
060 {
061 String string = (String) object;
062
063 if (string.length() < _minLength)
064 throw new ValidatorException(buildMessage(messages, field),
065 ValidationConstraint.MINIMUM_WIDTH);
066 }
067
068 protected String buildMessage(ValidationMessages messages, IFormComponent field)
069 {
070 return messages.formatValidationMessage(
071 getMessage(),
072 ValidationStrings.VALUE_TOO_SHORT,
073 new Object[]
074 { new Integer(_minLength), field.getDisplayName() });
075 }
076
077 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
078 FormComponentContributorContext context, IFormComponent field)
079 {
080 JSONObject profile = context.getProfile();
081
082 if (!profile.has(ValidationConstants.CONSTRAINTS)) {
083 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
084 }
085 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
086
087 accumulateProperty(cons, field.getClientId(),
088 new JSONLiteral("[tapestry.form.validation.isText,{"
089 + "minlength:" + _minLength + "}]"));
090
091 accumulateProfileProperty(field, profile,
092 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
093 }
094 }