Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.valid;
 
 import java.util.Map;
 
 import  org.apache.hivemind.HiveMind;
 import  org.apache.hivemind.Resource;
 import  org.apache.hivemind.impl.DefaultClassResolver;
 import  org.apache.hivemind.util.ClasspathResource;
 import  org.apache.hivemind.util.PropertyUtils;
Abstract base class for IValidator. Supports a required and locale property.

Author(s):
Howard Lewis Ship
Since:
1.0.8
 
 
 public abstract class BaseValidator implements IValidator
 {
    
Input Symbol used to represent the field being validated.

Since:
2.2
See also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map)
 
 
     public static final String FIELD_SYMBOL = "field";

    
Input symbol used to represent the validator itself to the script.

Since:
2.2
See also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map)
 
 
     public static final String VALIDATOR_SYMBOL = "validator";

    
Input symbol used to represent the IFormcontaining the field to the script.

Since:
2.2
See also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map)
 
 
     public static final String FORM_SYMBOL = "form";

    
Output symbol set by the script asthe name of the validator JavaScript function. The function implemented must return true or false (true if the field is valid, false otherwise). After the script is executed, the function is added to the IFormas a org.apache.tapestry.form.FormEventType.SUBMIT.

Since:
2.2
See also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map)
 
 
     public static final String FUNCTION_SYMBOL = "function";
 
     private boolean _required;

    

Since:
3.0
 
 
     private String _requiredMessage;

    

Since:
2.2
 
 
     private boolean _clientScriptingEnabled = false;
    
    

Since:
4.1
 
    private IScriptSource _scriptSource;
    
    
Standard constructor. Leaves locale as system default and required as false.
    public BaseValidator()
    {
    }

    
Allow the validator to be initialized with a property initialization string.

Since:
4.0
    public BaseValidator(String initializer)
    {
        PropertyUtils.configureProperties(thisinitializer);
    }
    protected BaseValidator(boolean required)
    {
         = required;
    }
    
    public boolean isRequired()
    {
        return ;
    }
    public void setRequired(boolean required)
    {
         = required;
    }
    
    public void setScriptSource(IScriptSource scriptSource)
    {
         = scriptSource;
    }
    
    
Gets a pattern, either as the default value, or as a localized key. If override is null, then the key from the org.apache.tapestry.valid.ValidationStrings ResourceBundle(in the specified locale) is used. The pattern can then be used with formatString(String, Object[]).

Why do we not just lump these strings into TapestryStrings.properties? because TapestryStrings.properties is localized to the server's locale, which is fine for the logging, debugging and error messages it contains. For field validation, whose errors are visible to the end user normally, we want to localize to the page's locale.

Parameters:
override The override value for the localized string from the bundle.
key used to lookup pattern from bundle, if override is null.
locale used to get right localization of bundle.
Since:
3.0
    protected String getPattern(String overrideString keyLocale locale)
    {
        if (override != null)
            return override;
        ResourceBundle strings = ResourceBundle.getBundle(
                "org.apache.tapestry.valid.ValidationStrings",
                locale);
        return strings.getString(key);
    }

    
Gets a string from the standard resource bundle. The string in the bundle is treated as a pattern for MessageFormat.format(java.lang.String, java.lang.Object[]).

Parameters:
pattern string the input pattern to be used with MessageFormat.format(java.lang.String, java.lang.Object[]). It may contain replaceable parameters, {0}, {1}, etc.
args the arguments used to fill replaceable parameters {0}, {1}, etc.
Since:
3.0
    protected String formatString(String patternObject[] args)
    {
        return MessageFormat.format(patternargs);
    }

    
Convienience method for invoking formatString(String, Object[]).

Since:
3.0
    protected String formatString(String patternObject arg)
    {
        return formatString(patternnew Object[]
        { arg });
    }

    
Convienience method for invoking formatString(String, Object[]).

Since:
3.0
    protected String formatString(String patternObject arg1Object arg2)
    {
        return formatString(patternnew Object[]
        { arg1arg2 });
    }

    
Invoked to check if the value is null. If the value is null (or empty), but the required flag is set, then this method throws a ValidatorException. Otherwise, returns true if the value is null.
    protected boolean checkRequired(IFormComponent fieldString valuethrows ValidatorException
    {
        boolean isEmpty = HiveMind.isBlank(value);
        if ( && isEmpty)
            throw new ValidatorException(buildRequiredMessage(field), .);
        return isEmpty;
    }

    
Builds an error message indicating a value for a required field was not supplied.

Since:
3.0
    protected String buildRequiredMessage(IFormComponent field)
    {
        String pattern = getPattern("field-is-required"field.getPage()
                .getLocale());
        return formatString(patternfield.getDisplayName());
    }

    
This implementation does nothing. Subclasses may supply their own implementation.

Since:
2.2
    public void renderValidatorContribution(IFormComponent fieldIMarkupWriter writer,
            IRequestCycle cycle)
    {
    }

    
Invoked (from sub-class implementations of renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle)to process a standard validation script. This expects that:
  • The IFormComponentis (ultimately) wrapped by a Body
  • The script generates a symbol named "function" (as per FUNCTION_SYMBOL)

Parameters:
scriptPath the resource path of the script to execute
cycle The active request cycle
field The field to be validated
symbols a set of input symbols needed by the script. These symbols are augmented with symbols for the field, form and validator. symbols may be null, but will be modified if not null.
Throws:
ApplicationRuntimeException if there's an error processing the script.
Since:
2.2
    protected void processValidatorScript(String scriptPathIRequestCycle cycle,
            IFormComponent fieldMap symbols)
    {
        IForm form = field.getForm();
        
        Map finalSymbols = (symbols == null) ? new HashMap() : symbols;
        
        finalSymbols.put(field);
        finalSymbols.put(form);
        finalSymbols.put(this);
        
        Resource location = new ClasspathResource(new DefaultClassResolver(), scriptPath);
        
        IScript script = .getScript(location);
        // If there's an error, report it against the field (this validator object doesn't
        // have a location).
        PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cyclefield);
        script.execute(fieldcyclepageRenderSupportfinalSymbols);
    }

    
Returns true if client scripting is enabled. Some validators are capable of generating client-side scripting to perform validation when the form is submitted. By default, this flag is false and subclasses should check it (in renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle)) before generating client side script.

Since:
2.2
    public boolean isClientScriptingEnabled()
    {
        return ;
    }
    public void setClientScriptingEnabled(boolean clientScriptingEnabled)
    {
         = clientScriptingEnabled;
    }
    public String getRequiredMessage()
    {
        return ;
    }

    
Overrides the field-is-required bundle key. Parameter {0} is the display name of the field.

Since:
3.0
    public void setRequiredMessage(String string)
    {
         = string;
    }
New to GrepCode? Check out our FAQ X