HFE: Validating forms

Hey @mseaton,

(Continuing here our discussions about validating HFE forms, as here on Slack for one of them.)

I am not sure where the form validation should happen exactly, maybe optionally just before here in HtmlFormEntryServiceImpl#saveHtmlForm.

Anyway my idea was to do something like that:

void validateForm(HtmlFormEntryService service, HtmlForm form) throws Exception {
    Document doc = HtmlFormEntryUtil.stringToDocument(form.getXmlData());
    
    Map<Node, TagValidator> validators = new HashMap<>();
    addTagValidators(service, HtmlFormEntryUtil.findChild(doc, "htmlform"), validators);
    
    validators.keySet().stream().forEach(node -> {
        validators.get(node).validate(node);
    });
}

Where basically I crawl through the nodes to get their TagValidator, and then validate each node.

As for the recursive addTagValidators it would look like this:

void addTagValidators(HtmlFormEntryService service, Node node, final Map<Node, TagValidator> validators) {
    
    TagHandler handler = service.getHandlerByTagName(node.getNodeName());
    
    if (handler != null) {
        if (handler instanceof TagValidator) {
            validators.put(node, (TagValidator) handler);
        }
        else {
            // unfortunately a handler leading to no validator
        }
    }
    
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        addTagValidators(service, list.item(i), validators);
    }
}

Does that make sense to you?

FYI this is syntactically correct code but I haven’t even tried it, I just pulled bits here and there taking a lot of inspiration from applyTagsHelper. I just wanted to kick start the topic and leave a trace for picking this up again next year :wink:

3 Likes