Search in sources :

Example 1 with GroupedDiagnositcs

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

the class WValidationErrors_Test method testGetGroupedErrors.

@Test
public void testGetGroupedErrors() {
    wValidationErrors.setLocked(true);
    UIContext uic = createUIContext();
    setActiveContext(uic);
    errors.clear();
    List<GroupedDiagnositcs> groupedErrors = wValidationErrors.getGroupedErrors();
    Assert.assertTrue("Should not have any groups by default", groupedErrors.isEmpty());
    WComponent component1 = new WTextField();
    WComponent component2 = new WTextField();
    Diagnostic component1Error1 = new DiagnosticImpl(Diagnostic.ERROR, component1, "Error 1");
    Diagnostic component2Error1 = new DiagnosticImpl(Diagnostic.ERROR, component2, "Error 2");
    Diagnostic component2Error2 = new DiagnosticImpl(Diagnostic.ERROR, component2, "Error 3");
    errors.add(component1Error1);
    errors.add(component2Error1);
    errors.add(component2Error2);
    wValidationErrors.setErrors(errors);
    groupedErrors = wValidationErrors.getGroupedErrors();
    Assert.assertEquals("Incorrect number of groups", 2, groupedErrors.size());
    GroupedDiagnositcs group1 = groupedErrors.get(0);
    GroupedDiagnositcs group2 = groupedErrors.get(1);
    Assert.assertEquals("Incorrect number of errors for group 1", 1, group1.getDiagnostics().size());
    Assert.assertSame("Incorrect diagnostic in group 1", component1Error1, group1.getDiagnostics().get(0));
    Assert.assertEquals("Incorrect number of errors for group 2", 2, group2.getDiagnostics().size());
    Assert.assertSame("Incorrect diagnostic in group 2", component2Error1, group2.getDiagnostics().get(0));
    Assert.assertSame("Incorrect diagnostic in group 2", component2Error2, group2.getDiagnostics().get(1));
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) UIContext(com.github.bordertech.wcomponents.UIContext) GroupedDiagnositcs(com.github.bordertech.wcomponents.validation.WValidationErrors.GroupedDiagnositcs) WTextField(com.github.bordertech.wcomponents.WTextField) Test(org.junit.Test)

Example 2 with GroupedDiagnositcs

use of com.github.bordertech.wcomponents.validation.WValidationErrors.GroupedDiagnositcs 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)

Aggregations

WComponent (com.github.bordertech.wcomponents.WComponent)2 GroupedDiagnositcs (com.github.bordertech.wcomponents.validation.WValidationErrors.GroupedDiagnositcs)2 UIContext (com.github.bordertech.wcomponents.UIContext)1 WContainer (com.github.bordertech.wcomponents.WContainer)1 WTextField (com.github.bordertech.wcomponents.WTextField)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)1 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)1 WValidationErrors (com.github.bordertech.wcomponents.validation.WValidationErrors)1 Test (org.junit.Test)1