001// Copyright 2004, 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
015package org.apache.tapestry.valid;
016
017import java.util.HashMap;
018import java.util.Map;
019
020import org.apache.tapestry.IMarkupWriter;
021import org.apache.tapestry.IRequestCycle;
022import org.apache.tapestry.form.IFormComponent;
023
024/**
025 * Simple validation of strings, to enforce required, and minimum length (maximum length is enforced
026 * in the client browser, by setting a maximum input length on the text field).
027 * 
028 * @author Howard Lewis Ship
029 * @since 1.0.8
030 */
031
032public class StringValidator extends BaseValidator
033{
034    private int _minimumLength;
035
036    private String _minimumLengthMessage;
037
038    /** @since 2.2 * */
039
040    private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script";
041
042    public StringValidator()
043    {
044    }
045
046    /**
047     * Initializes the StringValidator with properties defined by the initializer.
048     * 
049     * @since 4.0
050     */
051
052    public StringValidator(String initializer)
053    {
054        super(initializer);
055    }
056
057    public String toString(IFormComponent field, Object value)
058    {
059        if (value == null)
060            return null;
061
062        return value.toString();
063    }
064
065    public Object toObject(IFormComponent field, String input) throws ValidatorException
066    {
067        if (checkRequired(field, input))
068            return null;
069
070        if (_minimumLength > 0 && input.length() < _minimumLength)
071            throw new ValidatorException(buildMinimumLengthMessage(field),
072                    ValidationConstraint.MINIMUM_WIDTH);
073
074        return input;
075    }
076
077    public int getMinimumLength()
078    {
079        return _minimumLength;
080    }
081
082    public void setMinimumLength(int minimumLength)
083    {
084        _minimumLength = minimumLength;
085    }
086
087    /**
088     * @since 2.2
089     */
090
091    public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
092            IRequestCycle cycle)
093    {
094        if (!isClientScriptingEnabled())
095            return;
096
097        if (!(isRequired() || _minimumLength > 0))
098            return;
099
100        Map symbols = new HashMap();
101
102        if (isRequired())
103            symbols.put("requiredMessage", buildRequiredMessage(field));
104
105        if (_minimumLength > 0)
106            symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
107
108        processValidatorScript(_scriptPath, cycle, field, symbols);
109    }
110
111    /**
112     * @since 2.2
113     */
114
115    public String getScriptPath()
116    {
117        return _scriptPath;
118    }
119
120    /**
121     * Allows a developer to use the existing validation logic with a different client-side script.
122     * This is often sufficient to allow application-specific error presentation (perhaps by using
123     * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
124     * window than <code>window.alert()</code>).
125     * 
126     * @since 2.2
127     */
128
129    public void setScriptPath(String scriptPath)
130    {
131        _scriptPath = scriptPath;
132    }
133
134    /** @since 3.0 */
135    public String getMinimumLengthMessage()
136    {
137        return _minimumLengthMessage;
138    }
139
140    /**
141     * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
142     * Parameter {1} is the display name of the field.
143     * 
144     * @since 3.0
145     */
146
147    public void setMinimumLengthMessage(String string)
148    {
149        _minimumLengthMessage = string;
150    }
151
152    /** @since 3.0 */
153
154    protected String buildMinimumLengthMessage(IFormComponent field)
155    {
156        String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
157                .getLocale());
158
159        return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
160    }
161
162}