Search in sources :

Example 1 with Validator

use of com.qlangtech.tis.plugin.annotation.Validator in project tis by qlangtech.

the class PropertyType method isInputRequired.

public boolean isInputRequired() {
    if (inputRequired == null) {
        inputRequired = false;
        Validator[] validators = this.formField.validate();
        for (Validator v : validators) {
            if (v == Validator.require) {
                return inputRequired = true;
            }
        }
    }
    return inputRequired;
}
Also used : Validator(com.qlangtech.tis.plugin.annotation.Validator)

Example 2 with Validator

use of com.qlangtech.tis.plugin.annotation.Validator 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;
}
Also used : FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) AttrValMap(com.qlangtech.tis.util.AttrValMap) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) ImmutableMap(com.google.common.collect.ImmutableMap) AttrValMap(com.qlangtech.tis.util.AttrValMap) Validator(com.qlangtech.tis.plugin.annotation.Validator)

Aggregations

Validator (com.qlangtech.tis.plugin.annotation.Validator)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FormFieldType (com.qlangtech.tis.plugin.annotation.FormFieldType)1 AttrValMap (com.qlangtech.tis.util.AttrValMap)1 IOException (java.io.IOException)1