Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 // Copyright 2006, 2007, 2008 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.tapestry5;
Used by a Field to enforce a constraint related to a form submission. Validators themselves are stateless singletons.

Validators are usually encapsulated inside a FieldValidator.

public interface Validator<C, T>
{
    
Returns the type of constraint value used with this validator. Constraint values are used to parameterize a validator, for example a "maxLength" validator will have a constraint value of type int (the maximum length allowed). For constraints that do not have a constraint value, this method returns null.
    Class<C> getConstraintType();

    
Returns the value type associated with this validator. validate(org.apache.tapestry5.Field,java.lang.Object,org.apache.tapestry5.ioc.MessageFormatter,java.lang.Object) will only be invoked when the value is assignable to the validator's value type.
    Class<T> getValueType();

    
Returns the message key, within the validation messages, normally used by this validator. This is used to provide the org.apache.tapestry5.ioc.MessageFormatter passed to validate(org.apache.tapestry5.Field,java.lang.Object,org.apache.tapestry5.ioc.MessageFormatter,java.lang.Object) (unless overridden).

    String getMessageKey();

    
Invoked after the client-submitted value has been translated to check that the value conforms to expectations (often, in terms of minimum or maximum value). If and only if the value is approved by all Validators is the value applied by the field.

Parameters:
field the field for which a client submitted value is being validated
constraintValue the value used to constrain
formatter Validation messages, in the appropriate locale
value the translated value supplied by the user
Throws:
ValidationException if the value violates the constraint
    void validate(Field field, C constraintValueMessageFormatter formatter, T valuethrows ValidationException;

    
Returns true if the validator should be invoked for null or blank (empty string) values. This is generally false, but is true for validators that enforce that a non-blank value is required. This is the basis of the Field.isRequired() property.
    boolean isRequired();

    
Hook used by components to allow the validator to contribute additional attributes or (more often) client-side JavaScript (via the org.apache.tapestry5.services.FormSupport.addValidation(org.apache.tapestry5.Field,java.lang.String,java.lang.String,java.lang.Object)).

Parameters:
field the field which is currently being rendered
constraintValue the value used to constrain input
formatter validation message, in the appropriate locale
writer markup writer, allowing additional attributes to be written into the active element
formSupport used to add JavaScript
    void render(Field field, C constraintValueMessageFormatter formatterMarkupWriter writer,
                FormSupport formSupport);
New to GrepCode? Check out our FAQ X