Search in sources :

Example 11 with ValidationException

use of io.jmix.ui.component.ValidationException in project jmix by jmix-framework.

the class DoubleValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null) {
        return;
    }
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Double> datatype = Datatypes.getNN(Double.class);
            CurrentAuthentication currentAuthentication = AppBeans.get(CurrentAuthentication.class);
            Double num = datatype.parse((String) value, currentAuthentication.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 ? messageTools.loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : CurrentAuthentication(io.jmix.core.security.CurrentAuthentication) ValidationException(io.jmix.ui.component.ValidationException) ParseException(java.text.ParseException) BigDecimal(java.math.BigDecimal)

Example 12 with ValidationException

use of io.jmix.ui.component.ValidationException in project jmix by jmix-framework.

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("cuba_Messages");
        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(io.jmix.ui.component.ValidationException) CronSequenceGenerator(org.springframework.scheduling.support.CronSequenceGenerator) ValidationException(io.jmix.ui.component.ValidationException)

Example 13 with ValidationException

use of io.jmix.ui.component.ValidationException in project jmix by jmix-framework.

the class FutureOrPresentValidator method accept.

@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }
    TimeValidator timeConstraint = ValidatorHelper.getTimeConstraint(timeSource, value);
    if (timeConstraint == null) {
        throw new IllegalArgumentException("FutureOrPresentValidator doesn't support following type: '" + value.getClass() + "'");
    }
    timeConstraint.setCheckSeconds(checkSeconds);
    if (!timeConstraint.isFutureOrPresent()) {
        String message = getMessage();
        if (message == null) {
            message = messages.getMessage("validation.constraints.futureOrPresent");
        }
        throw new ValidationException(message);
    }
}
Also used : ValidationException(io.jmix.ui.component.ValidationException) TimeValidator(io.jmix.ui.component.validation.time.TimeValidator)

Example 14 with ValidationException

use of io.jmix.ui.component.ValidationException in project jmix by jmix-framework.

the class FutureValidator method accept.

@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }
    TimeValidator timeConstraint = ValidatorHelper.getTimeConstraint(timeSource, value);
    if (timeConstraint == null) {
        throw new IllegalArgumentException("FutureValidator doesn't support following type: '" + value.getClass() + "'");
    }
    timeConstraint.setCheckSeconds(checkSeconds);
    if (!timeConstraint.isFuture()) {
        String message = getMessage();
        if (message == null) {
            message = messages.getMessage("validation.constraints.future");
        }
        throw new ValidationException(message);
    }
}
Also used : ValidationException(io.jmix.ui.component.ValidationException) TimeValidator(io.jmix.ui.component.validation.time.TimeValidator)

Example 15 with ValidationException

use of io.jmix.ui.component.ValidationException in project jmix by jmix-framework.

the class MinValidator method accept.

@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }
    NumberConstraint constraint = getNumberConstraint(value);
    if (constraint == null || value instanceof Double || value instanceof Float) {
        throw new IllegalArgumentException("MinValidator doesn't support following type: '" + value.getClass() + "'");
    }
    if (!constraint.isMin(min)) {
        String message = getMessage();
        if (message == null) {
            message = messages.getMessage("validation.constraints.min");
        }
        String formattedValue = formatValue(value);
        String formattedMin = formatValue(min);
        throw new ValidationException(getTemplateErrorMessage(message, ParamsMap.of("value", formattedValue, "min", formattedMin)));
    }
}
Also used : NumberConstraint(io.jmix.ui.component.validation.number.NumberConstraint) ValidatorHelper.getNumberConstraint(io.jmix.ui.component.validation.ValidatorHelper.getNumberConstraint) ValidationException(io.jmix.ui.component.ValidationException)

Aggregations

ValidationException (io.jmix.ui.component.ValidationException)17 ValidatorHelper.getNumberConstraint (io.jmix.ui.component.validation.ValidatorHelper.getNumberConstraint)6 NumberConstraint (io.jmix.ui.component.validation.number.NumberConstraint)6 CurrentAuthentication (io.jmix.core.security.CurrentAuthentication)4 TimeValidator (io.jmix.ui.component.validation.time.TimeValidator)4 ParseException (java.text.ParseException)4 ServerInfoService (com.haulmont.cuba.core.app.ServerInfoService)1 Messages (com.haulmont.cuba.core.global.Messages)1 Datatype (io.jmix.core.metamodel.datatype.Datatype)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 CronSequenceGenerator (org.springframework.scheduling.support.CronSequenceGenerator)1 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)1 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)1