Package org.apache.tapestry

Examples of org.apache.tapestry.Validator


    @SuppressWarnings("unchecked")
    @Test
    public void multiple_validators_via_specification() throws Exception
    {
        ValidationMessagesSource messagesSource = newValidationMessagesSource();
        Validator required = newValidator();
        Validator minLength = newValidator();
        TypeCoercer coercer = newTypeCoercer();
        FieldComponent field = newFieldComponent();
        Messages messages = newMessages();
        MessageFormatter requiredFormatter = newMessageFormatter();
        MessageFormatter minLengthFormatter = newMessageFormatter();
        Object inputValue = "input value";
        ComponentResources resources = newComponentResources();
        Messages containerMessages = newMessages();
        Integer fifteen = 15;

        Map<String, Validator> map = newMap();

        map.put("required", required);
        map.put("minLength", minLength);

        train_getConstraintType(required, null);
        train_getConstraintType(minLength, Integer.class);

        train_getComponentResources(field, resources);
        train_getId(resources, "fred");
        train_getContainerMessages(resources, containerMessages);
        train_contains(containerMessages, "fred-required", false);

        train_getLocale(resources, Locale.FRENCH);

        train_getValidationMessages(messagesSource, Locale.FRENCH, messages);

        train_getMessageKey(required, "required");
        train_getMessageFormatter(messages, "required", requiredFormatter);

        train_contains(containerMessages, "fred-minLength", false);

        train_getMessageKey(minLength, "min-length");
        train_getMessageFormatter(messages, "min-length", minLengthFormatter);

        train_coerce(coercer, "15", Integer.class, fifteen);

        train_invokeIfBlank(required, true);
        train_getValueType(required, Object.class);
        required.validate(field, null, requiredFormatter, inputValue);

        train_invokeIfBlank(minLength, false);
        train_getValueType(minLength, String.class);
        minLength.validate(field, fifteen, minLengthFormatter, inputValue);

        replay();

        FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map);
View Full Code Here


    @SuppressWarnings("unchecked")
    @Test
    public void validator_with_constraint() throws Exception
    {
        ValidationMessagesSource messagesSource = newValidationMessagesSource();
        Validator validator = newValidator();
        TypeCoercer coercer = newTypeCoercer();
        FieldComponent field = newFieldComponent();
        Messages messages = newMessages();
        MessageFormatter formatter = newMessageFormatter();
        Object inputValue = new Object();
        ComponentResources resources = newComponentResources();
        Messages containerMessages = newMessages();
        Integer five = 5;

        Map<String, Validator> map = singletonMap("minLength", validator);

        train_getConstraintType(validator, Integer.class);

        train_coerce(coercer, "5", Integer.class, five);

        train_getComponentResources(field, resources);
        train_getId(resources, "fred");
        train_getContainerMessages(resources, containerMessages);
        train_contains(containerMessages, "fred-minLength", false);

        train_getLocale(resources, Locale.FRENCH);

        train_getValidationMessages(messagesSource, Locale.FRENCH, messages);

        train_getMessageKey(validator, "key");
        train_getMessageFormatter(messages, "key", formatter);

        train_invokeIfBlank(validator, false);
        train_getValueType(validator, Object.class);
        validator.validate(field, five, formatter, inputValue);

        replay();

        FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map);
View Full Code Here

    public FieldValidator createValidator(Field field, String validatorType,
            String constraintValue, String overrideId, Messages overrideMessages, Locale locale)
    {
        notBlank(validatorType, "validatorType");

        Validator validator = _validators.get(validatorType);

        if (validator == null)
            throw new IllegalArgumentException(ServicesMessages.unknownValidatorType(
                    validatorType,
                    InternalUtils.sortedKeys(_validators)));

        // I just have this thing about always treating parameters as finals, so
        // we introduce a second variable to treat a mutable.

        String finalConstraintValue = constraintValue;

        // If no constraint was provided, check to see if it is available via a localized message
        // key. This is really handy for complex validations such as patterns.

        if (finalConstraintValue == null && validator.getConstraintType() != null)
        {
            String key = overrideId + "-" + validatorType;

            if (overrideMessages.contains(key))
                finalConstraintValue = overrideMessages.get(key);
            else
                throw new IllegalArgumentException(ServicesMessages.missingValidatorConstraint(
                        validatorType,
                        validator.getConstraintType()));
        }

        Object coercedConstraintValue = coerceConstraintValue(finalConstraintValue, validator
                .getConstraintType());

        MessageFormatter formatter = findMessageFormatter(
                overrideId,
                overrideMessages,
View Full Code Here

TOP

Related Classes of org.apache.tapestry.Validator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.