Search in sources :

Example 21 with Diagnostic

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

the class WPasswordFieldRenderer method doRender.

/**
 * Paints the given WPasswordField.
 *
 * @param component the WPasswordField to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WPasswordField field = (WPasswordField) component;
    XmlStringBuilder xml = renderContext.getWriter();
    boolean readOnly = field.isReadOnly();
    xml.appendTagOpen(TAG_NAME);
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", component.isHidden(), "true");
    if (readOnly) {
        xml.appendAttribute("readOnly", "true");
        xml.appendEnd();
        return;
    }
    int cols = field.getColumns();
    int minLength = field.getMinLength();
    int maxLength = field.getMaxLength();
    WComponent submitControl = field.getDefaultSubmitButton();
    String submitControlId = submitControl == null ? null : submitControl.getId();
    xml.appendOptionalAttribute("disabled", field.isDisabled(), "true");
    xml.appendOptionalAttribute("required", field.isMandatory(), "true");
    xml.appendOptionalAttribute("minLength", minLength > 0, minLength);
    xml.appendOptionalAttribute("maxLength", maxLength > 0, maxLength);
    xml.appendOptionalAttribute("toolTip", field.getToolTip());
    xml.appendOptionalAttribute("accessibleText", field.getAccessibleText());
    xml.appendOptionalAttribute("size", cols > 0, cols);
    xml.appendOptionalAttribute("buttonId", submitControlId);
    xml.appendOptionalAttribute("placeholder", HtmlRenderUtil.getEffectivePlaceholder(field));
    List<Diagnostic> diags = field.getDiagnostics(Diagnostic.ERROR);
    if (diags == null || diags.isEmpty()) {
        xml.appendEnd();
        return;
    }
    xml.appendClose();
    DiagnosticRenderUtil.renderDiagnostics(field, renderContext);
    xml.appendEndTag(TAG_NAME);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WPasswordField(com.github.bordertech.wcomponents.WPasswordField) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 22 with Diagnostic

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

the class AbstractInput_Test method testValidateComponent.

@Test
public void testValidateComponent() {
    // No Validation
    AbstractInput input = new MyInput();
    List<Diagnostic> diags = new ArrayList<>();
    input.validate(diags);
    Assert.assertTrue("Input with no validation should return an empty daignostics list", diags.isEmpty());
    // Mandatory with no value - Validation Error
    input = new MyInput();
    diags.clear();
    input.setMandatory(true);
    input.validate(diags);
    Assert.assertEquals("Mandatory input with no value should return a diagnostic message", 1, diags.size());
    // Switch to ReadOnly and should have no validation errors
    input.setReadOnly(true);
    diags.clear();
    input.validate(diags);
    Assert.assertTrue("Mandatory input that is ReadOnly with no value should return an empty diagnostic message", diags.isEmpty());
    // Mandatory with a value - No Validation Errors
    input = new MyInput();
    diags.clear();
    input.setMandatory(true);
    input.setData("value");
    input.validate(diags);
    Assert.assertTrue("Mandatory input with a value should return an empty daignostics list", diags.isEmpty());
    // Custom Validator - Validation Error
    FieldValidator validator = new AbstractFieldValidator() {

        @Override
        protected boolean isValid() {
            return false;
        }
    };
    input = new MyInput();
    diags.clear();
    input.addValidator(validator);
    input.validate(diags);
    Assert.assertEquals("Input with custom validator should return a diagnostic message", 1, diags.size());
}
Also used : ArrayList(java.util.ArrayList) FieldValidator(com.github.bordertech.wcomponents.validator.FieldValidator) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) Test(org.junit.Test)

Example 23 with Diagnostic

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

the class WCardManager_Test method testRequestHandling.

@Test
public void testRequestHandling() {
    WCardManager manager = new WCardManager();
    MockContainer cardOne = new MockContainer();
    MockContainer cardTwo = new MockContainer();
    manager.add(cardOne);
    manager.add(cardTwo);
    manager.setLocked(true);
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getHandleRequestCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getValidateCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPreparePaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getHandleRequestCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getValidateCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPreparePaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPaintCount());
    MockRequest request = new MockRequest();
    setActiveContext(createUIContext());
    PrintWriter writer = new XmlStringBuilder(new StringWriter());
    List<Diagnostic> diags = new ArrayList<>();
    manager.serviceRequest(request);
    Assert.assertEquals("Card One should be visible and therefore called", 1, cardOne.getHandleRequestCount());
    Assert.assertEquals("Card Two should be invisible and therefore not called", 0, cardTwo.getHandleRequestCount());
    cardOne.reset();
    manager.makeVisible(cardTwo);
    manager.serviceRequest(request);
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getHandleRequestCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getHandleRequestCount());
    cardTwo.reset();
    manager.validateComponent(diags);
    manager.preparePaint(request);
    manager.paint(new WebXmlRenderContext(writer));
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getValidateCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPreparePaintCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPaintCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getValidateCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPreparePaintCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPaintCount());
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 24 with Diagnostic

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

the class WValidationErrorsRenderer_Test method testDoPaintBasic.

@Test
public void testDoPaintBasic() throws IOException, SAXException, XpathException {
    WValidationErrors errors = new WValidationErrors();
    WTextArea text1 = new WTextArea();
    text1.setText("text1");
    WContainer root = new WContainer();
    root.add(errors);
    root.add(text1);
    root.setLocked(true);
    // Validate Schema with no errors
    assertSchemaMatch(root);
    // Simulate Error Message
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, text1, "Test Error1"));
    root.showErrorIndicators(diags);
    errors.setErrors(diags);
    assertSchemaMatch(root);
    assertXpathEvaluatesTo(text1.getId(), "//ui:validationerrors/ui:error/@for", root);
    assertXpathEvaluatesTo("Test Error1", "//ui:validationerrors/ui:error", root);
    String title = "WValidationErrorsTitle";
    errors.setTitleText(title);
    assertSchemaMatch(root);
    assertXpathEvaluatesTo(title, "//ui:validationerrors/@title", root);
    // Check for error message with no associated component
    setActiveContext(createUIContext());
    diags.clear();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, null, "Test Error1"));
    errors.setErrors(diags);
    assertSchemaMatch(root);
    assertXpathNotExists("//ui:validationerrors/ui:error/@for", root);
    assertXpathEvaluatesTo("Test Error1", "//ui:validationerrors/ui:error", root);
}
Also used : WTextArea(com.github.bordertech.wcomponents.WTextArea) WContainer(com.github.bordertech.wcomponents.WContainer) 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) Test(org.junit.Test)

Example 25 with Diagnostic

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

the class WFieldErrorIndicatorRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    WContainer root = new WContainer();
    WPanel target = new WPanel();
    WFieldErrorIndicator indicator = new WFieldErrorIndicator(target);
    root.add(target);
    root.add(indicator);
    // Simulate Error Message
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, target, "Test Error"));
    root.showErrorIndicators(diags);
    // Validate Schema
    assertSchemaMatch(root);
    // Check Attributes
    assertXpathEvaluatesTo(indicator.getId(), "//ui:fieldindicator/@id", root);
    assertXpathEvaluatesTo("error", "//ui:fieldindicator/@type", root);
    assertXpathEvaluatesTo(target.getId(), "//ui:fieldindicator/@for", root);
    // Check Message
    assertXpathEvaluatesTo("Test Error", "//ui:fieldindicator/ui:message", 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)

Aggregations

Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)34 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)24 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)9 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)7 WContainer (com.github.bordertech.wcomponents.WContainer)6 WComponent (com.github.bordertech.wcomponents.WComponent)5 WPanel (com.github.bordertech.wcomponents.WPanel)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 WValidationErrors (com.github.bordertech.wcomponents.validation.WValidationErrors)3 WDateField (com.github.bordertech.wcomponents.WDateField)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 WFieldErrorIndicator (com.github.bordertech.wcomponents.validation.WFieldErrorIndicator)2 WFieldWarningIndicator (com.github.bordertech.wcomponents.validation.WFieldWarningIndicator)2 Date (java.util.Date)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)1 Diagnosable (com.github.bordertech.wcomponents.Diagnosable)1 Input (com.github.bordertech.wcomponents.Input)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)1