mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-05 04:38:10 +08:00
add ability to pass parameters to script validator
This commit is contained in:
parent
820d7834c9
commit
087e796c17
@ -16,6 +16,7 @@ import com.haulmont.cuba.core.global.ScriptingProvider;
|
||||
import com.haulmont.cuba.core.global.MessageUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@ -27,6 +28,8 @@ public class ScriptValidator implements Field.Validator {
|
||||
private String scriptPath;
|
||||
private boolean innerScript;
|
||||
|
||||
private Map<String, Object> params;
|
||||
|
||||
public ScriptValidator(Element element, String messagesPack) {
|
||||
this.script = element.getText();
|
||||
innerScript = StringUtils.isNotBlank(script);
|
||||
@ -43,12 +46,24 @@ public class ScriptValidator implements Field.Validator {
|
||||
this.scriptPath = scriptPath;
|
||||
}
|
||||
|
||||
public ScriptValidator(String scriptPath, String message, String messagesPack, Map<String, Object> params) {
|
||||
this.scriptPath = scriptPath;
|
||||
this.message = message;
|
||||
this.messagesPack = messagesPack;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public void validate(Object value) throws ValidationException {
|
||||
Boolean isValid = false;
|
||||
if (params == null) {
|
||||
params = Collections.singletonMap("value", value);
|
||||
} else {
|
||||
params.put("value", value);
|
||||
}
|
||||
if (innerScript) {
|
||||
isValid = ScriptingProvider.evaluateGroovy(ScriptingProvider.Layer.GUI, script, Collections.<String, Object>singletonMap("value", value));
|
||||
isValid = ScriptingProvider.evaluateGroovy(ScriptingProvider.Layer.GUI, script, params);
|
||||
} else if (scriptPath != null) {
|
||||
isValid = ScriptingProvider.runGroovyScript(scriptPath, Collections.<String, Object>singletonMap("value", value));
|
||||
isValid = ScriptingProvider.runGroovyScript(scriptPath, params);
|
||||
}
|
||||
if (!isValid) {
|
||||
String msg = message != null ? MessageUtils.loadString(messagesPack, message) : "Invalid value '%s'";
|
||||
|
Loading…
Reference in New Issue
Block a user