Search in sources :

Example 16 with Diagnostic

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

the class DateFieldPivotValidator_Test method runTest.

/**
 * @param operator the date operator
 * @return the test results
 */
private String[][] runTest(final int operator) {
    String[][] result = new String[DATES.length][DATES[0].length];
    for (int run = 0; run < DATES.length; run++) {
        DateFieldPivotValidator validator = new DateFieldPivotValidator(operator);
        validator.setFixedPivot(DATES[run][2]);
        WDateField dateField = new WDateField();
        dateField.addValidator(validator);
        List<Diagnostic> diags = new ArrayList<>();
        for (int i = 0; i < DATES[run].length; i++) {
            diags.clear();
            dateField.setDate(DATES[run][i]);
            dateField.validate(diags);
            Assert.assertTrue("Should only have a maximum of 1 error", diags.size() < 2);
            if (!diags.isEmpty()) {
                result[run][i] = (diags.get(0)).getDescription();
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) WDateField(com.github.bordertech.wcomponents.WDateField) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic)

Example 17 with Diagnostic

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

the class Validation_Test method testMandatory.

@Test
public void testMandatory() {
    List<Diagnostic> diags = new ArrayList<>();
    WTextField textField = new WTextField();
    Assert.assertFalse("Text field should not be mandatory by default", textField.isMandatory());
    // No validation logic yet, but that should still be ok.
    textField.validate(diags);
    Assert.assertEquals("Diags should be empty when there are no validators", 0, diags.size());
    // Add a mandatory validation
    textField.setMandatory(true);
    Assert.assertTrue("Text field should be mandatory", textField.isMandatory());
    textField.validate(diags);
    Assert.assertEquals("Diags should contain mandatory validation error", 1, diags.size());
    // Add some text to the text field
    textField.setText("Blah");
    diags.clear();
    textField.validate(diags);
    Assert.assertEquals("Diags should be empty when mandatory field is filled in", 0, diags.size());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WTextField(com.github.bordertech.wcomponents.WTextField) Test(org.junit.Test)

Example 18 with Diagnostic

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

the class DiagnosticRenderUtil method renderHelper.

/**
 * Render the diagnostics.
 * @param renderContext the current renderContext
 * @param diags the list of Diagnostic objects
 * @param severity the severity we are rendering
 */
private static void renderHelper(final WebXmlRenderContext renderContext, final List<Diagnostic> diags, final int severity) {
    if (diags.isEmpty()) {
        return;
    }
    XmlStringBuilder xml = renderContext.getWriter();
    xml.appendTagOpen(TAG_NAME);
    xml.appendAttribute("type", getLevel(severity));
    xml.appendClose();
    for (Diagnostic diagnostic : diags) {
        xml.appendTag(MESSAGE_TAG_NAME);
        xml.appendEscaped(diagnostic.getDescription());
        xml.appendEndTag(MESSAGE_TAG_NAME);
    }
    xml.appendEndTag(TAG_NAME);
}
Also used : Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 19 with Diagnostic

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

the class WFileWidgetRenderer method doRender.

/**
 * Paints the given WFileWidget.
 *
 * @param component the WFileWidget to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WFileWidget fileWidget = (WFileWidget) component;
    XmlStringBuilder xml = renderContext.getWriter();
    boolean readOnly = fileWidget.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;
    }
    xml.appendOptionalAttribute("disabled", fileWidget.isDisabled(), "true");
    xml.appendOptionalAttribute("required", fileWidget.isMandatory(), "true");
    xml.appendOptionalAttribute("toolTip", fileWidget.getToolTip());
    xml.appendOptionalAttribute("accessibleText", fileWidget.getAccessibleText());
    xml.appendOptionalAttribute("acceptedMimeTypes", typesToString(fileWidget.getFileTypes()));
    long maxFileSize = fileWidget.getMaxFileSize();
    xml.appendOptionalAttribute("maxFileSize", maxFileSize > 0, maxFileSize);
    List<Diagnostic> diags = fileWidget.getDiagnostics(Diagnostic.ERROR);
    if (diags == null || diags.isEmpty()) {
        xml.appendEnd();
        return;
    }
    xml.appendClose();
    DiagnosticRenderUtil.renderDiagnostics(fileWidget, renderContext);
    xml.appendEndTag(TAG_NAME);
}
Also used : WFileWidget(com.github.bordertech.wcomponents.WFileWidget) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 20 with Diagnostic

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

the class WMultiFileWidgetRenderer method doRender.

/**
 * Paints the given WMultiFileWidget.
 *
 * @param component the WMultiFileWidget to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WMultiFileWidget widget = (WMultiFileWidget) component;
    XmlStringBuilder xml = renderContext.getWriter();
    // Check if rendering a file upload response
    String uploadId = widget.getFileUploadRequestId();
    if (uploadId != null) {
        handleFileUploadRequest(widget, xml, uploadId);
        return;
    }
    boolean readOnly = widget.isReadOnly();
    xml.appendTagOpen(TAG_NAME);
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", widget.isHidden(), "true");
    if (readOnly) {
        xml.appendAttribute("readOnly", "true");
    } else {
        long maxFileSize = widget.getMaxFileSize();
        int maxFiles = widget.getMaxFiles();
        WComponent dropzone = widget.getDropzone();
        WImageEditor editor = widget.getEditor();
        xml.appendOptionalAttribute("disabled", widget.isDisabled(), "true");
        xml.appendOptionalAttribute("required", widget.isMandatory(), "true");
        xml.appendOptionalAttribute("toolTip", widget.getToolTip());
        xml.appendOptionalAttribute("accessibleText", widget.getAccessibleText());
        xml.appendOptionalAttribute("acceptedMimeTypes", typesToString(widget.getFileTypes()));
        xml.appendOptionalAttribute("maxFileSize", maxFileSize > 0, maxFileSize);
        xml.appendOptionalAttribute("maxFiles", maxFiles > 0, maxFiles);
        if (dropzone != null) {
            xml.appendAttribute("dropzone", dropzone.getId());
        }
        if (editor != null) {
            xml.appendAttribute("editor", editor.getId());
            if (editor.getUseCamera()) {
                xml.appendAttribute("camera", true);
            }
        }
    }
    if (widget.getColumns() != null) {
        xml.appendAttribute("cols", widget.getColumns());
    }
    if (widget.getFileAjaxAction() != null) {
        xml.appendAttribute("ajax", "true");
    }
    List<Diagnostic> diags = readOnly ? null : widget.getDiagnostics(Diagnostic.ERROR);
    if (widget.getFiles().isEmpty()) {
        if (readOnly || diags == null || diags.isEmpty()) {
            xml.appendEnd();
            return;
        }
        xml.appendClose();
    } else {
        xml.appendClose();
        // Render files
        int i = 0;
        for (FileWidgetUpload file : widget.getFiles()) {
            FileWidgetRendererUtil.renderFileElement(widget, xml, file, i++);
        }
    }
    if (!readOnly && diags != null && !diags.isEmpty()) {
        DiagnosticRenderUtil.renderDiagnostics(widget, renderContext);
    }
    xml.appendEndTag(TAG_NAME);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WImageEditor(com.github.bordertech.wcomponents.WImageEditor) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WMultiFileWidget(com.github.bordertech.wcomponents.WMultiFileWidget) FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

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