Package com.opensymphony.xwork2.validator

Examples of com.opensymphony.xwork2.validator.ValidatorContext


        addValidationErrors(violations.toArray(new ConstraintViolation[violations.size()]), action, valueStack, null);
    }

  private void addValidationErrors(ConstraintViolation[] violations, Object action, ValueStack valueStack, String parentFieldname) {
    if (violations != null) {
            ValidatorContext validatorContext = new DelegatingValidatorContext(action);
            for (ConstraintViolation violation : violations) {
                //translate message
                String key = violation.getMessage();

                String message = key;
                // push context variable into stack, to allow use ${max}, ${min} etc in error messages
                valueStack.push(violation.getMessageVariables());
                //push the validator into the stack
                valueStack.push(violation.getContext());
                try {
                    message = validatorContext.getText(key);
                } finally {
                    valueStack.pop();
                    valueStack.pop();
                }

                if (isActionError(violation)) {
                    if (LOG.isDebugEnabled()) {
                  LOG.debug("Adding action error '#0'", message);
                    }
                    validatorContext.addActionError(message);
                } else {
                    ValidationError validationError = buildValidationError(violation, message);

                    // build field name
                    String fieldName = validationError.getFieldName();
                    if (parentFieldname != null) {
                      fieldName = parentFieldname + "." + fieldName;
                    }

                    if (LOG.isDebugEnabled()) {
                  LOG.debug("Adding field error [#0] with message '#1'", fieldName, validationError.getMessage());
                    }
                    validatorContext.addFieldError(fieldName, validationError.getMessage());

                    // don't add "model." prefix to fields of model in model driven action
                    if ((action instanceof ModelDriven) && "model".equals(fieldName)) {
                      fieldName = null;
                    }
View Full Code Here


        addValidationErrors(violations.toArray(new ConstraintViolation[0]), action, valueStack, null);
    }

  private void addValidationErrors(ConstraintViolation[] violations, Object action, ValueStack valueStack, String parentFieldname) {
    if (violations != null) {
            ValidatorContext validatorContext = new DelegatingValidatorContext(action);
            for (ConstraintViolation violation : violations) {
                //translate message
                String key = violation.getMessage();

                //push the validator into the stack
                valueStack.push(violation.getContext());
                String message = key;
                try {
                    message = validatorContext.getText(key);
                } finally {
                    valueStack.pop();
                }

                if (isActionError(violation)) {
                    if (LOG.isDebugEnabled()) {
                  LOG.debug("Adding action error '#0'", message);
                    }
                    validatorContext.addActionError(message);
                } else {
                    ValidationError validationError = buildValidationError(violation, message);

                    // build field name
                    String fieldName = validationError.getFieldName();
                    if (parentFieldname != null) {
                      fieldName = parentFieldname + "." + fieldName;
                    }

                    if (LOG.isDebugEnabled()) {
                  LOG.debug("Adding field error [#0] with message '#1'", fieldName, validationError.getMessage());
                    }
                    validatorContext.addFieldError(fieldName, validationError.getMessage());

                    // don't add "model." prefix to fields of model in model driven action
                    if ((action instanceof ModelDriven) && "model".equals(fieldName)) {
                      fieldName = null;
                    }
View Full Code Here

    private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
        ValueStack stack = ActionContext.getContext().getValueStack();
        stack.push(o);

        ValidatorContext validatorContext;

        if (appendPrefix) {
            validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
        } else {
            ValidatorContext parent = getValidatorContext();
            validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
        }

        actionValidatorManager.validate(o, visitorContext, validatorContext);
        stack.pop();
View Full Code Here

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        action = new VisitorValidatorTestAction();
        ValidatorContext vc1 = new DelegatingValidatorContext(action);

        VisitorFieldValidator.AppendingValidatorContext vc2 = new AppendingValidatorContext(
                vc1, "value", FIRST_NAME, "");
        validatorContext = new AppendingValidatorContext(vc2, "value", SECOND_NAME, "");
    }
View Full Code Here

public class DateRangeFieldValidatorTest extends XWorkTestCase {

    public void testPassValidation() throws Exception {
        // given
        ValidationAction action = prepareAction(createDate(2013, 6, 6));
        ValidatorContext context = new GenericValidatorContext(action);
        DateRangeFieldValidator validator = prepareValidator(action, context);

        // when
        validator.validate(action);

        // then
        assertTrue(context.getFieldErrors().size() == 0);
    }
View Full Code Here

    }

    public void testMinValidation() throws Exception {
        // given
        ValidationAction action = prepareAction(createDate(2012, Calendar.MARCH, 3));
        ValidatorContext context = new GenericValidatorContext(action);
        DateRangeFieldValidator validator = prepareValidator(action, context);

        // when
        validator.validate(action);

        // then
        assertTrue(context.getFieldErrors().size() == 1);
        assertEquals("Max is 12.12.13, min is 01.01.13 but value is 03.03.12", context.getFieldErrors().get("dateRange").get(0));
    }
View Full Code Here

    }

    public void testMaxValidation() throws Exception {
        // given
        ValidationAction action = prepareAction(createDate(2014, Calendar.APRIL, 4));
        ValidatorContext context = new GenericValidatorContext(action);
        DateRangeFieldValidator validator = prepareValidator(action, context);

        // when
        validator.validate(action);

        // then
        assertTrue(context.getFieldErrors().size() == 1);
        assertEquals("Max is 12.12.13, min is 01.01.13 but value is 04.04.14", context.getFieldErrors().get("dateRange").get(0));
    }
View Full Code Here

    public void validate( Object obj )
        throws ValidationException
    {

        String method = (String) getFieldValue( "method", obj );
        ValidatorContext ctxt = getValidatorContext();

        if ( method.equals( "rsync" ) )
        {
            String rsyncHost = (String) getFieldValue( "rsyncHost", obj );
            if ( rsyncHost == null || rsyncHost.equals( "" ) )
            {
                ctxt.addActionError( "Rsync host is required." );
            }

            String rsyncDirectory = (String) getFieldValue( "rsyncDirectory", obj );
            if ( rsyncDirectory == null || rsyncDirectory.equals( "" ) )
            {
                ctxt.addActionError( "Rsync directory is required." );
            }

            String rsyncMethod = (String) getFieldValue( "rsyncMethod", obj );
            if ( rsyncMethod == null || rsyncMethod.equals( "" ) )
            {
                ctxt.addActionError( "Rsync method is required." );
            }
            else
            {
                if ( !rsyncMethod.equals( "anonymous" ) && !rsyncMethod.equals( "ssh" ) )
                {
                    ctxt.addActionError( "Invalid rsync method" );
                }
            }

            String username = (String) getFieldValue( "username", obj );
            if ( username == null || username.equals( "" ) )
            {
                ctxt.addActionError( "Username is required." );
            }

        }
        else if ( method.equals( "svn" ) )
        {
            String svnUrl = (String) getFieldValue( "svnUrl", obj );
            if ( svnUrl == null || svnUrl.equals( "" ) )
            {
                ctxt.addActionError( "SVN url is required." );
            }

            String username = (String) getFieldValue( "username", obj );
            if ( username == null || username.equals( "" ) )
            {
                ctxt.addActionError( "Username is required." );
            }
        }
        else if ( method.equals( "cvs" ) )
        {
            String cvsRoot = (String) getFieldValue( "cvsRoot", obj );
            if ( cvsRoot == null || cvsRoot.equals( "" ) )
            {
                ctxt.addActionError( "CVS root is required." );
            }
        }
        else if ( method.equals( "file" ) )
        {
            String directory = (String) getFieldValue( "directory", obj );
            if ( directory == null || directory.equals( "" ) )
            {
                ctxt.addActionError( "Directory is required." );
            }
        }

        if ( ctxt.hasActionErrors() )
        {
            return;
        }
    }
View Full Code Here

        String snapshotsPolicy = (String) getFieldValue( "snapshotsPolicy", obj );
        String releasesPolicy = (String) getFieldValue( "releasesPolicy", obj );
        Integer snapshotsInterval = (Integer) getFieldValue( "snapshotsInterval", obj );
        Integer releasesInterval = (Integer) getFieldValue( "releasesInterval", obj );

        ValidatorContext ctxt = getValidatorContext();

        if ( !snapshotsPolicy.equals( "interval" ) )
        {
            if ( snapshotsInterval.intValue() != 0 )
            {
                ctxt.addActionError( "Snapshots Interval must be set to zero." );
            }
        }

        if ( !releasesPolicy.equals( "interval" ) )
        {
            if ( releasesInterval.intValue() != 0 )
            {
                ctxt.addActionError( "Releases Interval must be set to zero." );
            }
        }

        if ( ctxt.hasActionErrors() )
        {
            return;
        }
    }
View Full Code Here

    private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
        ValueStack stack = ActionContext.getContext().getValueStack();
        stack.push(o);

        ValidatorContext validatorContext;

        if (appendPrefix) {
            validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
        } else {
            ValidatorContext parent = getValidatorContext();
            validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
        }

        actionValidatorManager.validate(o, visitorContext, validatorContext);
        stack.pop();
View Full Code Here

TOP

Related Classes of com.opensymphony.xwork2.validator.ValidatorContext

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.