use of com.qlangtech.tis.util.AttrValMap in project plugins by qlangtech.
the class TestRockMqPluginValidate method validatePluginPostForm.
private void validatePluginPostForm(String jsonPath, Context context) throws IOException {
// DefaultFieldErrorHandler fieldErrorHandler = new DefaultFieldErrorHandler();
DelegateControl4JsonPostMsgHandler fieldErrorHandler = null;
List<AttrValMap> attrValMaps = null;
try {
try (InputStream reader = this.getClass().getResourceAsStream(jsonPath)) {
JSONArray itemsArray = JSONArray.parseArray(IOUtils.toString(reader, TisUTF8.get()));
attrValMaps = AttrValMap.describableAttrValMapList(fieldErrorHandler, itemsArray, Optional.empty());
}
} catch (Exception e) {
throw new IllegalStateException("jsonPath:" + jsonPath, e);
}
assertNotNull(attrValMaps);
assertEquals(1, attrValMaps.size());
AttrValMap attrValMap = attrValMaps.get(0);
Descriptor.PluginValidateResult validateResult = attrValMap.validate(context, true);
assertFalse("validate false", validateResult.isValid());
}
use of com.qlangtech.tis.util.AttrValMap in project tis by qlangtech.
the class Descriptor method isValid.
private boolean isValid(IControlMsgHandler msgHandler, Context context, boolean bizValidate, Optional<IPropertyType.SubFormFilter> subFormFilter, PluginFormProperties propertyTypes, PostFormVals postFormVals) {
Objects.requireNonNull(postFormVals, "postFormVals can not be null");
Map<String, JSONObject> formData = postFormVals.rawFormData;
boolean valid = true;
String attr;
PropertyType attrDesc;
JSONObject valJ;
String impl;
String attrVal;
for (Map.Entry<String, PropertyType> entry : propertyTypes.getKVTuples()) {
attr = entry.getKey();
attrDesc = entry.getValue();
valJ = formData.get(attr);
if (valJ == null && attrDesc.isInputRequired()) {
addFieldRequiredError(msgHandler, context, attr);
valid = false;
continue;
}
if (valJ == null) {
valJ = new JSONObject();
}
if (attrDesc.isDescribable()) {
JSONObject descVal = valJ.getJSONObject("descVal");
impl = descVal.getString("impl");
if (StringUtils.isBlank(impl)) {
addFieldRequiredError(msgHandler, context, attr);
valid = false;
continue;
}
AttrValMap attrValMap = AttrValMap.parseDescribableMap(msgHandler, subFormFilter, descVal);
pushFieldStack(context, attr, 0);
try {
if (!attrValMap.validate(context, bizValidate).isValid()) {
valid = false;
continue;
}
} finally {
popFieldStack(context);
}
} else {
if (attrDesc.typeIdentity() == FormFieldType.MULTI_SELECTABLE.getIdentity()) {
List<FormFieldType.SelectedItem> selectedItems = getSelectedMultiItems(valJ);
// }
if (selectedItems.size() < 1) {
// 没有选中
Validator[] validators = attrDesc.getValidator();
for (Validator v : validators) {
if (v == Validator.require) {
v.validate(msgHandler, context, attr, StringUtils.EMPTY);
}
}
} else if (this instanceof FormFieldType.IMultiSelectValidator) {
FormFieldType.IMultiSelectValidator multiSelectValidator = (FormFieldType.IMultiSelectValidator) this;
if (!multiSelectValidator.validate(msgHandler, subFormFilter, context, attr, selectedItems)) {
valid = false;
break;
}
}
} else {
// single value
boolean containVal = valJ.containsKey(KEY_primaryVal);
if (!containVal && attrDesc.isInputRequired()) {
addFieldRequiredError(msgHandler, context, attr);
valid = false;
continue;
}
if (containVal) {
attrVal = valJ.getString(KEY_primaryVal);
postFormVals.fieldVals.put(attr, attrVal);
Validator[] validators = attrDesc.getValidator();
for (Validator v : validators) {
if (!v.validate(msgHandler, context, attr, attrVal)) {
valid = false;
break;
}
}
try {
Method validateMethod = this.validateMethodMap.get(attr);
if (validateMethod != null && StringUtils.isNotEmpty(attrVal)) {
if (!(boolean) validateMethod.invoke(this, msgHandler, context, attr, attrVal)) {
valid = false;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
}
// end for
return valid;
}
Aggregations