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