Example usage for org.apache.wicket.validation ValidationError getVariables

List of usage examples for org.apache.wicket.validation ValidationError getVariables

Introduction

In this page you can find the example usage for org.apache.wicket.validation ValidationError getVariables.

Prototype

public final Map<String, Object> getVariables() 

Source Link

Document

Retrieves the variables map for this error.

Usage

From source file:com.premiumminds.webapp.wicket.validators.HibernateValidatorProperty.java

License:Open Source License

public void validate(IValidatable<Object> validatable) {
    Validator validator = HibernateValidatorProperty.validatorFactory.getValidator();

    @SuppressWarnings("unchecked")
    Set<ConstraintViolation<Object>> violations = validator.validateValue(
            (Class<Object>) beanModel.getObject().getClass(), propertyName, validatable.getValue());

    if (!violations.isEmpty()) {
        for (ConstraintViolation<?> violation : violations) {
            ValidationError error = new ValidationError(violation.getMessage());

            String key = violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName();
            if (getValidatorPrefix() != null)
                key = getValidatorPrefix() + "." + key;

            error.addKey(key);//from  w  w w. ja  v  a  2 s.c  o  m
            error.setVariables(
                    new HashMap<String, Object>(violation.getConstraintDescriptor().getAttributes()));

            //remove garbage from the attributes
            error.getVariables().remove("payload");
            error.getVariables().remove("message");
            error.getVariables().remove("groups");

            validatable.error(error);
        }
    }
}

From source file:org.brixcms.web.util.validators.NodeNameValidator.java

License:Apache License

@SuppressWarnings("unchecked")
public void validate(IValidatable validatable) {
    String s = validatable.getValue().toString();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (isForbidden(c)) {
            String forbiddenStr = Arrays.toString(forbidden);
            forbiddenStr = forbiddenStr.substring(1, forbiddenStr.length() - 1);
            ValidationError error = new ValidationError();
            error.setMessage("Field ${label} may not contain any of the forbidden characters (${forbidden}).");
            error.addMessageKey("NodeNameValidator");
            error.getVariables().put("forbidden", forbiddenStr);
            validatable.error(error);//from   ww w  .j  av  a2 s .  c  o  m
            return;
        }
    }
}

From source file:org.brixcms.web.util.validators.NodePathValidator.java

License:Apache License

@SuppressWarnings("unchecked")
public void validate(IValidatable validatable) {
    Object o = validatable.getValue();
    if (o != null) {
        JcrNode node = nodeModel.getObject();
        Path path = null;//from  w  w w . j  a v  a  2  s .co  m
        if (o instanceof Path) {
            path = (Path) o;
        } else {
            path = new Path(o.toString());
        }

        if (!path.isAbsolute()) {
            Path parent = new Path(node.getPath());
            if (!((BrixNode) node).isFolder())
                parent = parent.parent();
            path = parent.append(path);
        } else {
            path = new Path(SitePlugin.get().toRealWebNodePath(path.toString()));
        }
        if (node.getSession().itemExists(path.toString()) == false) {
            ValidationError error = new ValidationError();
            error.setMessage("Node ${path} could not be found");
            error.addMessageKey("NodePathValidator");
            error.getVariables().put("path", path.toString());
            validatable.error(error);
        }
    }
}

From source file:org.jabylon.rest.ui.wicket.validators.TerminologyProjectValidator.java

License:Open Source License

@Override
public void validate(IValidatable<Boolean> validatable) {
    if (validatable.getValue()) {
        Project object = project.getObject();
        Workspace workspace = object.getParent();
        if (workspace == null) {
            if (project instanceof AttachableModel) {
                AttachableModel<Project> model = (AttachableModel<Project>) project;
                workspace = (Workspace) model.getParent().getObject();
            }/*from  w w  w.  j  a  v  a 2s  .  c o  m*/
        }
        if (workspace != null) {
            EList<Project> children = workspace.getChildren();
            for (Project other : children) {
                if (other == object)
                    continue;
                if (other.isTerminology()) {
                    //only one terminology project allowed atm
                    ValidationError error = new ValidationError(this);
                    error.getVariables().put("name", other.getName());
                    validatable.error(error);
                }
            }
        }
    }

}

From source file:org.jabylon.rest.ui.wicket.validators.UniqueNameValidator.java

License:Open Source License

@Override
public void validate(IValidatable<String> validatable) {
    if (usedNames.contains(validatable.getValue())) {
        ValidationError error = new ValidationError(this);
        error.getVariables().put("name", validatable.getValue());
        validatable.error(error);//www .j a va  2  s  .co  m
    }

}

From source file:org.obiba.onyx.wicket.data.DataValidatorTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//w  w w .  j  a  va2 s .c  o  m
public void testDataValidatorWithInvalidType() {
    Date testDate = new Date();
    Validatable value = new Validatable();
    value.setValue(DataBuilder.buildDate(testDate));

    // Expect a DECIMAL, but obtain an DATE
    DataValidator validator = new DataValidator(mockValidator, DataType.DECIMAL);
    // Expect no calls on mockValidator
    EasyMock.replay(mockValidator);
    validator.validate(value);
    EasyMock.verify(mockValidator);

    List<IValidationError> errors = value.getErrors();
    Assert.assertNotNull(errors);
    Assert.assertEquals(1, errors.size());
    IValidationError ierror = errors.get(0);
    Assert.assertTrue(ierror instanceof ValidationError);
    ValidationError error = (ValidationError) ierror;
    Map<String, Object> variables = error.getVariables();
    Assert.assertTrue(variables.containsKey("expectedType"));
    Assert.assertEquals(DataType.DECIMAL, variables.get("expectedType"));

    Assert.assertTrue(variables.containsKey("actualType"));
    Assert.assertEquals(DataType.DATE, variables.get("actualType"));

    Assert.assertTrue(variables.containsKey("value"));
    Assert.assertEquals(testDate, variables.get("value"));

    // We can only get at the list of keys by asking the error for its message
    IErrorMessageSource mockSource = EasyMock.createMock(IErrorMessageSource.class);
    EasyMock.expect(mockSource.getMessage(EasyMock.eq("DataValidator.incompatibleTypes")))
            .andReturn("theMessage");
    EasyMock.expectLastCall().once();
    // We are not testing the IValidationError implementation, but we need to expect this call anyway. Make it as
    // flexible as possible. The important expectation is the first which expects the proper message key.
    EasyMock.expect(mockSource.substitute((String) EasyMock.anyObject(), (Map) EasyMock.anyObject()))
            .andReturn("theSubistutedMessage");
    EasyMock.expectLastCall().anyTimes();

    EasyMock.replay(mockSource);
    error.getErrorMessage(mockSource);
    EasyMock.verify(mockSource);
}

From source file:org.wicketstuff.multitextinput.MultiTextInput.java

License:Apache License

/**
 * Robbed from {@link FormComponent}//from w  w  w.  jav  a2 s  .  c om
 * 
 * @param e
 * @param error
 */
private void reportValidationError(ConversionException e, ValidationError error) {
    final Locale locale = e.getLocale();
    if (locale != null) {
        error.setVariable("locale", locale);
    }
    error.setVariable("exception", e);
    Format format = e.getFormat();
    if (format instanceof SimpleDateFormat) {
        error.setVariable("format", ((SimpleDateFormat) format).toLocalizedPattern());
    }

    Map<String, Object> variables = e.getVariables();
    if (variables != null) {
        error.getVariables().putAll(variables);
    }

    error(error);
}