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

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

Introduction

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

Prototype

public ValidationError addKey(IValidator<?> validator) 

Source Link

Document

Shortcut for adding a standard message key which is the simple name of the validator' class

Usage

From source file:com.alfredmuponda.lostandfound.util.ItemNumberValidator.java

License:Apache License

public void error(IValidatable validatable, String errorKey) {
    ValidationError error = new ValidationError();
    error.addKey(getClass().getSimpleName() + "." + errorKey);
    validatable.error(error);/*  ww  w.  ja  va  2 s .  c o  m*/
}

From source file:com.chitek.ignition.drivers.generictcp.meta.config.ui.MessageConfigUI.java

License:Apache License

private void handleOnUpload(InputStream inputStream) {

    try {//from  w w w .  jav a  2 s.  c  om
        StringBuilder sb = new StringBuilder();

        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = br.readLine()) != null)
            sb.append(line);
        br.close();

        MessageConfig importConfig = MessageConfig.fromXMLString(sb.toString());

        // Increment imported message ID if necessary to make it unique
        int newMessageId = getNextMessageId(importConfig.getMessageId() - 1);
        if (newMessageId != importConfig.getMessageId()) {
            importConfig.setMessageId(newMessageId);
            warn(new StringResourceModel("warn.importIdChanged", this, null, currentMessageId).getString());
        }
        getConfig().addMessageConfig(importConfig);
        currentMessage = importConfig;
        currentMessageId = importConfig.getMessageId();

        // Refresh the model data in the list editor
        currentMessageIdDropdown.clearInput();
        messageIdTextField.clearInput();
        messageAliasTextField.clearInput();
        editor.reloadModel();

        // Make sure the new alias is unique (can't be done in the validator)
        for (MessageConfig message : getConfig().getMessageList()) {
            if (importConfig.getMessageAlias().equalsIgnoreCase(message.getMessageAlias())) {
                ValidationError error = new ValidationError();
                error.addKey(UniqueMessageAliasValidator.messageKey);
                messageAliasTextField.error(error);
                break;
            }
        }

        info(new StringResourceModel("info.import", this, null, currentMessageId).getString());

    } catch (Exception e) {
        error(getString("import.error") + " Exception: " + e.toString());
    }

}

From source file:com.chitek.wicket.listeditor.UniqueListItemValidator.java

License:Apache License

/**
 * Validator for items in {@link ListEditor}. Validates the input to be unique among all
 * entrys in the list. /*from   ww w  .  j a  va  2  s  . c  o  m*/
 */
@SuppressWarnings("rawtypes")
@Override
public void validate(IValidatable<T> validatable) {

    ListEditor<?> editor = parent.findParent(ListEditor.class);
    String id = parent.getId();

    int count = 0;
    List<FormComponent> fields = editor.getComponentsById(id, FormComponent.class);

    String parentValue = getValue(validatable);

    if (filter != null && caseSensitive && !filter.contains(parentValue))
        return;
    if (filter != null && !caseSensitive && !filter.contains(parentValue.toLowerCase()))
        return;
    if (filter != null && !caseSensitive && !filter.contains(parentValue.toUpperCase()))
        return;

    for (FormComponent field : fields) {
        String value = field.getRawInput() != null ? field.getRawInput().toString() : null;

        if (value != null
                && (caseSensitive ? value.equals(parentValue) : value.equalsIgnoreCase(parentValue))) {
            count++;
        }
    }

    if (count > 1) {
        ValidationError error = new ValidationError();
        error.addKey(messageKey);
        validatable.error(error);
    }
}

From source file:com.chitek.wicket.NonMatchStringValidator.java

License:Apache License

@Override
public void validate(IValidatable<String> validatable) {
    String value = validatable.getValue();
    for (String match : Arrays.asList(matches)) {
        if (value.equalsIgnoreCase(match)) {
            ValidationError error = new ValidationError();
            error.addKey("StringValidator.noMatch");
            validatable.error(error);/*from w w  w .  jav a2  s.co  m*/
        }
    }
}

From source file:com.googlecode.wicket.jquery.ui.form.slider.RangeSlider.java

License:Apache License

@Override
protected void convertInput() {
    Integer lower = this.lower.getConvertedInput();
    Integer upper = this.upper.getConvertedInput();

    if (lower != null && upper != null) {
        RangeValue value = new RangeValue(lower, upper);

        if (value.getLower() <= value.getUpper()) {
            this.setConvertedInput(value);
        } else {//from  www . j ava 2 s  .com
            ValidationError error = new ValidationError();
            error.addKey("RangeSlider.ConversionError"); //wicket6
            error.setVariable("lower", value.getLower());
            error.setVariable("upper", value.getUpper());

            this.error(error);
        }
    }
}

From source file:com.googlecode.wicket.jquery.ui.kendo.datetime.DateTimePicker.java

License:Apache License

@Override
protected void convertInput() {
    final IConverter<Date> converter = this.getConverter(Date.class);

    String dateInput = this.datePicker.getInput();
    String timeInput = this.timePicker.getInput();

    try {//from   ww w . java2s. c o  m
        Date date = converter.convertToObject(this.formatInput(dateInput, timeInput), this.getLocale());
        this.setConvertedInput(date);
    } catch (ConversionException e) {
        ValidationError error = new ValidationError();
        error.addKey("DateTimePicker.ConversionError"); //wicket6
        error.setVariable("date", dateInput);
        error.setVariable("time", timeInput);

        this.error(error);
    }
}

From source file:com.googlecode.wicket.kendo.ui.form.datetime.AjaxDateTimePicker.java

License:Apache License

@Override
public void convertInput() {
    final IConverter<Date> converter = this.getConverter(Date.class);

    String dateInput = this.datePicker.getInput();
    String timeInput = this.timePicker.getInput();

    try {/* w w w .  j  ava  2 s  .  c om*/
        String value = this.formatInput(dateInput, timeInput);
        this.setConvertedInput(converter.convertToObject(value, this.getLocale()));
    } catch (ConversionException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage(), e);
        }

        ValidationError error = new ValidationError();
        error.addKey("AjaxDateTimePicker.ConversionError"); // wicket6
        error.setVariable("date", dateInput);
        error.setVariable("time", timeInput);

        this.error(error);
    }
}

From source file:com.googlecode.wicket.kendo.ui.form.datetime.local.AjaxDateTimePicker.java

License:Apache License

@Override
public void convertInput() {
    final IConverter<LocalDateTime> converter = this.getConverter(LocalDateTime.class);

    String dateInput = this.datePicker.getInput();
    String timeInput = this.timePicker.getInput();

    try {//from   ww w . j  av  a2s.co m
        String value = this.formatInput(dateInput, timeInput);
        this.setConvertedInput(converter.convertToObject(value, this.getLocale()));
    } catch (ConversionException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage(), e);
        }

        ValidationError error = new ValidationError();
        error.addKey("AjaxDateTimePicker.ConversionError"); // wicket6
        error.setVariable("date", dateInput);
        error.setVariable("time", timeInput);

        this.error(error);
    }
}

From source file:com.inductiveautomation.xopc.drivers.modbus2.configuration.web.ModbusConfigurationUI.java

License:Open Source License

private TextField<String> newPrefixTextField(final ModbusConfigurationEntry configEntry) {
    final RequiredTextField<String> textField = new RequiredTextField<String>("prefix",
            new PropertyModel<String>(configEntry, "designatorRange.designator"));

    textField.add(new PatternValidator("[A-Za-z0-9_]+") {
        @Override//from w  ww  .j a v  a  2  s . co  m
        protected ValidationError decorate(ValidationError error, IValidatable<String> validatable) {
            error.addKey("prefix.PatternValidator");
            return error;
        }
    });

    textField.add(new StringValidator(1, 12) {
        @Override
        protected ValidationError decorate(ValidationError error, IValidatable<String> validatable) {
            error.addKey("prefix.LengthValidator");
            return error;
        }
    });

    textField.add(new PrefixValidator());

    textField.add(new OnChangeAjaxBehavior() {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            configEntry.getDesignatorRange().setDesignator(textField.getModelObject());
        }
    });

    return textField;
}

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);
            error.setVariables(//from ww w  . j  a v a2s .  c  om
                    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);
        }
    }
}