Search in sources :

Example 6 with ValidationException

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

the class BulkEditorWindow method applyChanges.

public void applyChanges() {
    if (validateAll()) {
        StringBuilder sb = new StringBuilder();
        if (modelValidators != null) {
            for (Consumer moduleValidator : modelValidators) {
                try {
                    moduleValidator.accept(datasource);
                } catch (ValidationException e) {
                    sb.append(e.getMessage());
                    sb.append("\n");
                }
            }
        }
        if (sb.length() == 0) {
            List<String> fields = new ArrayList<>();
            for (Map.Entry<String, Field> fieldEntry : dataFields.entrySet()) {
                Field field = fieldEntry.getValue();
                if (isFieldChanged(field)) {
                    String localizedName = managedFields.get(fieldEntry.getKey()).getLocalizedName();
                    fields.add("- " + localizedName);
                }
            }
            if (!fields.isEmpty()) {
                showConfirmDialogOrCommit(fields);
            } else {
                showNotification(messages.getMessage("io.jmix.ui.app.bulk/bulk.noChanges"), NotificationType.HUMANIZED);
            }
        } else {
            showNotification(sb.toString(), NotificationType.TRAY);
        }
    }
}
Also used : ValidationException(io.jmix.ui.component.ValidationException) Consumer(java.util.function.Consumer)

Example 7 with ValidationException

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

the class IntegerValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null) {
        return;
    }
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Integer> datatype = Datatypes.getNN(Integer.class);
            CurrentAuthentication currentAuthentication = AppBeans.get(CurrentAuthentication.class);
            Integer num = datatype.parse((String) value, currentAuthentication.getLocale());
            result = checkIntegerOnPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Integer && checkIntegerOnPositive((Integer) 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)

Example 8 with ValidationException

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

the class LongValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null) {
        return;
    }
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Long> datatype = Datatypes.getNN(Long.class);
            CurrentAuthentication currentAuthentication = AppBeans.get(CurrentAuthentication.class);
            Long num = datatype.parse((String) value, currentAuthentication.getLocale());
            result = checkPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Long && checkPositive((Long) 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)

Example 9 with ValidationException

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

the class ScriptValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    Boolean isValid = false;
    if (params == null) {
        params = new HashMap<>();
    }
    params.put("value", value);
    Map<String, Object> arguments = new HashMap<>();
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        arguments.put(entry.getKey(), entry.getValue());
    }
    if (innerScript) {
        isValid = (Boolean) scriptEvaluator.evaluate(new StaticScriptSource(script), arguments);
    } else if (scriptPath != null) {
        isValid = (Boolean) scriptEvaluator.evaluate(new ResourceScriptSource(resources.getResource(scriptPath)), arguments);
    }
    if (!isValid) {
        String msg = message != null ? messageTools.loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ValidationException(io.jmix.ui.component.ValidationException) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with ValidationException

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

the class DateValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null)
        return;
    boolean result;
    if (value instanceof String) {
        try {
            Datatype datatype = Datatypes.getNN(java.sql.Date.class);
            CurrentAuthentication currentAuthentication = AppBeans.get(CurrentAuthentication.class);
            datatype.parse((String) value, currentAuthentication.getLocale());
            result = true;
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Date;
    }
    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) Date(java.util.Date) Datatype(io.jmix.core.metamodel.datatype.Datatype)

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