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

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

Introduction

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

Prototype

public final ValidationError setVariables(Map<String, Object> vars) 

Source Link

Document

Sets 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 .  j av a  2s.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.apache.openmeetings.core.util.StrongPasswordValidator.java

License:Apache License

private void error(IValidatable<String> pass, String key, Map<String, Object> params) {
    if (web) {/*from  w w w.ja  v  a2 s  .  c  o m*/
        ValidationError err = new ValidationError().addKey(key);
        if (params != null) {
            err.setVariables(params);
        }
        pass.error(err);
    } else {
        String msg = LabelDao.getString(key, 1L);
        if (params != null && !params.isEmpty() && !Strings.isEmpty(msg)) {
            for (Map.Entry<String, Object> e : params.entrySet()) {
                msg = msg.replace(String.format("${%s}", e.getKey()), "" + e.getValue());
            }
        }
        log.warn(msg);
        pass.error(new ValidationError(msg));
    }
}

From source file:org.cast.cwm.data.validator.UniqueDataFieldValidator.java

License:Open Source License

@Override
public void validate(final IValidatable<T> validatable) {
    HibernateObjectModel<PersistedObject> other = new HibernateObjectModel<PersistedObject>(clazz,
            new CriteriaBuilder() {

                @Override//from ww  w . j a va 2 s  .c  o m
                public void build(Criteria criteria) {
                    criteria.add(Restrictions.eq(field, validatable.getValue()));
                    for (String field : scope.keySet()) {
                        criteria.add(Restrictions.eq(field, scope.get(field).getObject()));
                    }
                }
            });

    if (other.getObject() != null && !other.getObject().equals(current.getObject())) {
        ValidationError err = new ValidationError(this).addKey(getResourceKey());
        err.setVariables(variablesMap());
        validatable.error(err);
    }
}

From source file:org.yes.cart.web.page.component.customer.dynaform.editor.StringEditor.java

License:Apache License

/**
 * Construct simple text editor.//from  ww  w. j av a2s  . c o  m
 *
 * @param id         editor id.
 * @param markupProvider markup object.
 * @param model      model.
 * @param labelModel label model
 * @param errorLabelModel error label model
 * @param attrValue  {@link org.yes.cart.domain.entity.AttrValue}
 * @param readOnly  if true this component is read only
 */
public StringEditor(final String id, final MarkupContainer markupProvider, final IModel<String> model,
        final IModel<String> labelModel, final IModel<String> errorLabelModel, final AttrValue attrValue,
        final boolean readOnly) {

    super(id, "stringEditor", markupProvider);

    final TextField textField = new TextField(EDIT, model);
    textField.setLabel(labelModel);
    textField.setRequired(attrValue.getAttribute().isMandatory());
    textField.setEnabled(!readOnly);
    if (StringUtils.isNotBlank(attrValue.getAttribute().getRegexp())) {
        final PatternValidator patternValidator = new PatternValidator(attrValue.getAttribute().getRegexp()) {

            /** {@inheritDoc}*/
            public boolean validateOnNullValue() {
                return attrValue.getAttribute().isMandatory();
            }

            /** {@inheritDoc}*/
            public void error(final IValidatable<String> validatable, final String resourceKey,
                    final Map<String, Object> vars) {

                if (validatable == null) {
                    throw new IllegalArgumentException("Argument [[validatable]] cannot be null");
                }
                if (vars == null) {
                    throw new IllegalArgumentException("Argument [[vars]] cannot be null");
                }

                ValidationError error = new ValidationError();
                error.setMessage(errorLabelModel.getObject());
                error.setVariables(vars);
                validatable.error(error);
            }
        };

        textField.add(patternValidator);
    }
    textField.add(new AttributeModifier("placeholder", labelModel));
    add(textField);
}