@Override public void execute(final ActionEvent event) { field2.setMandatory(!field2.isMandatory()); field2.getLabel().setHint( "mandatory and must be at least two characters and may only contain alphabetic characters and must be different from field 1"); } });
@Test public void testSetMandatory() { WTextField textField = new WTextField(); WField field = new WField("dummy", textField); Assert.assertFalse("Field should not be mandatory by default", textField.isMandatory()); field.setMandatory(true); Assert.assertTrue("Field should be mandatory after setMandatory(true)", textField. isMandatory()); field.setMandatory(false); Assert.assertFalse("Field should be mandatory after setMandatory(true)", textField. isMandatory()); }
@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()); }
xml.appendOptionalAttribute("required", textField.isMandatory(), "true"); xml.appendOptionalAttribute("minLength", minLength > 0, minLength); xml.appendOptionalAttribute("maxLength", maxLength > 0, maxLength);