Search in sources :

Example 1 with ValidationException

use of com.haulmont.cuba.gui.components.ValidationException in project cuba by cuba-platform.

the class DoubleValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Double> datatype = Datatypes.getNN(Double.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Double num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkDoubleOnPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = (value instanceof Double && checkDoubleOnPositive((Double) value)) || (value instanceof BigDecimal && checkBigDecimalOnPositive((BigDecimal) value));
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException) BigDecimal(java.math.BigDecimal)

Example 2 with ValidationException

use of com.haulmont.cuba.gui.components.ValidationException in project cuba by cuba-platform.

the class IntegerValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Integer> datatype = Datatypes.getNN(Integer.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Integer num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkIntegerOnPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Integer && checkIntegerOnPositive((Integer) value);
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException)

Example 3 with ValidationException

use of com.haulmont.cuba.gui.components.ValidationException in project cuba by cuba-platform.

the class LongValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Long> datatype = Datatypes.getNN(Long.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Long num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Long && checkPositive((Long) value);
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException)

Example 4 with ValidationException

use of com.haulmont.cuba.gui.components.ValidationException in project cuba by cuba-platform.

the class SizeWithUnitValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null) {
        return;
    }
    MessageTools messages = AppBeans.get(MessageTools.NAME);
    if (!(value instanceof String)) {
        throw new ValidationException(messages.loadString(messagesPack, message));
    }
    String s = (String) value;
    s = s.trim();
    if ("".equals(value)) {
        return;
    }
    Matcher matcher = Pattern.compile(SIZE_PATTERN).matcher(s);
    if (!matcher.find()) {
        throw new ValidationException(messages.loadString(messagesPack, message));
    }
    double size = Double.parseDouble(matcher.group(1));
    String symbol = matcher.group(2);
    if ("%".equals(symbol)) {
        if (size < 1 || size > MAX_SIZE_PERCENTS) {
            throw new ValidationException(messages.loadString(messagesPack, message));
        }
    } else if ("px".equals(symbol) || symbol == null) {
        if (size < 1 || size > MAX_SIZE_PIXELS) {
            throw new ValidationException(messages.loadString(messagesPack, message));
        }
    }
}
Also used : MessageTools(com.haulmont.cuba.core.global.MessageTools) ValidationException(com.haulmont.cuba.gui.components.ValidationException) Matcher(java.util.regex.Matcher)

Example 5 with ValidationException

use of com.haulmont.cuba.gui.components.ValidationException in project cuba by cuba-platform.

the class CronValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value != null) {
        ServerInfoService serverInfoService = AppBeans.get(ServerInfoService.NAME);
        Messages messages = AppBeans.get(Messages.NAME);
        try {
            new CronSequenceGenerator(value.toString(), serverInfoService.getTimeZone());
        } catch (Exception e) {
            throw new ValidationException(messages.getMessage(CronValidator.class, "validation.cronInvalid"));
        }
    }
}
Also used : ServerInfoService(com.haulmont.cuba.core.app.ServerInfoService) Messages(com.haulmont.cuba.core.global.Messages) ValidationException(com.haulmont.cuba.gui.components.ValidationException) CronSequenceGenerator(org.springframework.scheduling.support.CronSequenceGenerator) ValidationException(com.haulmont.cuba.gui.components.ValidationException)

Aggregations

ValidationException (com.haulmont.cuba.gui.components.ValidationException)8 ParseException (java.text.ParseException)5 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)4 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 ServerInfoService (com.haulmont.cuba.core.app.ServerInfoService)1 MessageTools (com.haulmont.cuba.core.global.MessageTools)1 Messages (com.haulmont.cuba.core.global.Messages)1 Scripting (com.haulmont.cuba.core.global.Scripting)1 RequiredValueMissingException (com.haulmont.cuba.gui.components.RequiredValueMissingException)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 CronSequenceGenerator (org.springframework.scheduling.support.CronSequenceGenerator)1