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));
}
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");
}
}
Aggregations