Search in sources :

Example 1 with DiagnosticImpl

use of com.github.bordertech.wcomponents.validation.DiagnosticImpl in project wcomponents by BorderTech.

the class WValidationErrorsRenderer method doRender.

/**
 * Paints the given {@link WValidationErrors} component.
 *
 * @param component The {@link WValidationErrors} component to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WValidationErrors errors = (WValidationErrors) component;
    XmlStringBuilder xml = renderContext.getWriter();
    if (errors.hasErrors()) {
        xml.appendTagOpen("ui:validationerrors");
        xml.appendAttribute("id", component.getId());
        xml.appendOptionalAttribute("class", component.getHtmlClass());
        xml.appendOptionalAttribute("track", component.isTracking(), "true");
        xml.appendOptionalAttribute("title", errors.getTitleText());
        xml.appendClose();
        for (GroupedDiagnositcs nextGroup : errors.getGroupedErrors()) {
            // Render each diagnostic message in this group.
            for (Diagnostic nextMessage : nextGroup.getDiagnostics()) {
                xml.appendTagOpen("ui:error");
                WComponent forComponent = nextMessage.getComponent();
                if (forComponent != null) {
                    UIContextHolder.pushContext(nextMessage.getContext());
                    try {
                        xml.appendAttribute("for", forComponent.getId());
                    } finally {
                        UIContextHolder.popContext();
                    }
                }
                xml.appendClose();
                // of a WComponent as the message.
                if (nextMessage instanceof DiagnosticImpl) {
                    WComponent messageComponent = ((DiagnosticImpl) nextMessage).createDiagnosticErrorComponent();
                    // We add the component to a throw-away container so that it renders with the correct ID.
                    WContainer container = new WContainer() {

                        @Override
                        public String getId() {
                            return component.getId();
                        }
                    };
                    container.add(messageComponent);
                    messageComponent.paint(renderContext);
                    container.remove(messageComponent);
                    container.reset();
                } else {
                    xml.append(nextMessage.getDescription());
                }
                xml.appendEndTag("ui:error");
            }
        }
        xml.appendEndTag("ui:validationerrors");
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WContainer(com.github.bordertech.wcomponents.WContainer) DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WValidationErrors(com.github.bordertech.wcomponents.validation.WValidationErrors) GroupedDiagnositcs(com.github.bordertech.wcomponents.validation.WValidationErrors.GroupedDiagnositcs) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 2 with DiagnosticImpl

use of com.github.bordertech.wcomponents.validation.DiagnosticImpl in project wcomponents by BorderTech.

the class WValidationErrorsRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WValidationErrors errors = new WValidationErrors();
    String content = getMaliciousContent();
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, new DefaultWComponent(), content));
    errors.setErrors(diags);
    assertSafeContent(errors);
}
Also used : DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WValidationErrors(com.github.bordertech.wcomponents.validation.WValidationErrors) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) DefaultWComponent(com.github.bordertech.wcomponents.DefaultWComponent) Test(org.junit.Test)

Example 3 with DiagnosticImpl

use of com.github.bordertech.wcomponents.validation.DiagnosticImpl in project wcomponents by BorderTech.

the class WFieldErrorIndicatorRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WContainer root = new WContainer();
    WPanel target = new WPanel();
    WFieldErrorIndicator indicator = new WFieldErrorIndicator(target);
    root.add(indicator);
    root.add(target);
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, target, getMaliciousContent()));
    root.showErrorIndicators(diags);
    assertSafeContent(root);
}
Also used : WFieldErrorIndicator(com.github.bordertech.wcomponents.validation.WFieldErrorIndicator) WContainer(com.github.bordertech.wcomponents.WContainer) DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WPanel(com.github.bordertech.wcomponents.WPanel) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 4 with DiagnosticImpl

use of com.github.bordertech.wcomponents.validation.DiagnosticImpl in project wcomponents by BorderTech.

the class WFieldWarningIndicatorRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    WContainer root = new WContainer();
    WPanel target = new WPanel();
    WFieldWarningIndicator indicator = new WFieldWarningIndicator(target);
    root.add(target);
    root.add(indicator);
    // Simulate Warning Message
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.WARNING, target, "Test Warning"));
    root.showWarningIndicators(diags);
    // Validate Schema
    assertSchemaMatch(root);
    // Check Attributes
    assertXpathEvaluatesTo(indicator.getId(), "//ui:fieldindicator/@id", root);
    assertXpathEvaluatesTo("warn", "//ui:fieldindicator/@type", root);
    assertXpathEvaluatesTo(target.getId(), "//ui:fieldindicator/@for", root);
    // Check Message
    assertXpathEvaluatesTo("Test Warning", "//ui:fieldindicator/ui:message", root);
}
Also used : WContainer(com.github.bordertech.wcomponents.WContainer) DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WPanel(com.github.bordertech.wcomponents.WPanel) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WFieldWarningIndicator(com.github.bordertech.wcomponents.validation.WFieldWarningIndicator) Test(org.junit.Test)

Example 5 with DiagnosticImpl

use of com.github.bordertech.wcomponents.validation.DiagnosticImpl in project wcomponents by BorderTech.

the class BasicFields method validateComponent.

/**
 * An example of cross field validation.
 */
@Override
protected void validateComponent(final List<Diagnostic> diags) {
    String text1 = field1.getText();
    String text2 = field2.getText();
    String text3 = field3.getText();
    if (text1 != null && text1.length() > 0 && text1.equals(text2)) {
        // Note that this error will hyperlink to Field 2.
        diags.add(createErrorDiagnostic(field2, "Fields 1 and 2 cannot be the same."));
    }
    int len = 0;
    if (text1 != null) {
        len += text1.length();
    }
    if (text2 != null) {
        len += text2.length();
    }
    if (len > 20) {
        // Note that this error does not link to a specific field.
        diags.add(createErrorDiagnostic("The total length of Field 1 plus Field 2 can exceed 20 characters."));
    }
    // Sample Warning Message
    if (Util.empty(text3)) {
        diags.add(new DiagnosticImpl(Diagnostic.WARNING, UIContextHolder.getCurrent(), field3, "Warning that this should not be blank"));
    }
}
Also used : DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl)

Aggregations

DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)11 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 WContainer (com.github.bordertech.wcomponents.WContainer)6 WPanel (com.github.bordertech.wcomponents.WPanel)4 WValidationErrors (com.github.bordertech.wcomponents.validation.WValidationErrors)3 WFieldErrorIndicator (com.github.bordertech.wcomponents.validation.WFieldErrorIndicator)2 WFieldWarningIndicator (com.github.bordertech.wcomponents.validation.WFieldWarningIndicator)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WField (com.github.bordertech.wcomponents.WField)1 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)1 WTextArea (com.github.bordertech.wcomponents.WTextArea)1 WTextField (com.github.bordertech.wcomponents.WTextField)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 GroupedDiagnositcs (com.github.bordertech.wcomponents.validation.WValidationErrors.GroupedDiagnositcs)1 Serializable (java.io.Serializable)1