diff options
author | Artur Signell <artur@vaadin.com> | 2015-05-01 19:44:40 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-05-19 06:45:45 +0000 |
commit | d63bf2c215831dcfea060331339199cc086d78f7 (patch) | |
tree | b077f4c2f3702680c56fcff5a64a57169b3d776e | |
parent | 851306745e0974669403ea0e9d07b08e9fbf979a (diff) | |
download | vaadin-framework-d63bf2c215831dcfea060331339199cc086d78f7.tar.gz vaadin-framework-d63bf2c215831dcfea060331339199cc086d78f7.zip |
Make fields with validators immediate (#13709)7.5.0.beta1
Change-Id: I113aa38869cf4dceec24315e70d3b44eae526f65
3 files changed, 95 insertions, 4 deletions
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index fd8a72ee23..27d97d5e03 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -405,6 +405,26 @@ public abstract class AbstractComponent extends AbstractClientConnector } } + /** + * Returns the explicitly set immediate value. + * + * @return the explicitly set immediate value or null if + * {@link #setImmediate(boolean)} has not been explicitly invoked + */ + protected Boolean getExplicitImmediateValue() { + return explicitImmediateValue; + } + + /** + * Returns the immediate mode of the component. + * <p> + * Certain operations such as adding a value change listener will set the + * component into immediate mode if {@link #setImmediate(boolean)} has not + * been explicitly called with false. + * + * @return true if the component is in immediate mode (explicitly or + * implicitly set), false if the component if not in immediate mode + */ public boolean isImmediate() { if (explicitImmediateValue != null) { return explicitImmediateValue; diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index 5c02c9e5fb..cf14d1cb96 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1745,6 +1745,24 @@ public abstract class AbstractField<T> extends AbstractComponent implements } } + /** + * {@inheritDoc} + * <p> + * Fields are automatically set to immediate if validators have been added. + */ + @Override + public boolean isImmediate() { + if (getExplicitImmediateValue() != null) { + return getExplicitImmediateValue(); + } + // Make field immediate when there is some kind of validation present + // (validator or required). This will avoid the UI being in a wrong + // state, e.g. user entered valid data but old validation error is still + // shown + return super.isImmediate() || !getValidators().isEmpty() + || isRequired(); + } + /* * (non-Javadoc) * diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValidatorsTest.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValidatorsTest.java index 61aafe317d..59831d92e1 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValidatorsTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValidatorsTest.java @@ -1,16 +1,19 @@ package com.vaadin.tests.server.component.abstractfield; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.easymock.EasyMock; +import org.junit.Test; import com.vaadin.data.Validator; import com.vaadin.ui.AbstractField; -import com.vaadin.ui.Field; -public class AbsFieldValidatorsTest extends TestCase { +public class AbsFieldValidatorsTest { - Field<Object> field = new AbstractField<Object>() { + AbstractField<Object> field = new AbstractField<Object>() { @Override public Class getType() { return Object.class; @@ -20,6 +23,7 @@ public class AbsFieldValidatorsTest extends TestCase { Validator validator = EasyMock.createMock(Validator.class); Validator validator2 = EasyMock.createMock(Validator.class); + @Test public void testAddValidator() { assertNotNull(field.getValidators()); assertEquals(0, field.getValidators().size()); @@ -34,6 +38,7 @@ public class AbsFieldValidatorsTest extends TestCase { assertTrue(field.getValidators().contains(validator2)); } + @Test public void testRemoveValidator() { field.addValidator(validator); field.addValidator(validator2); @@ -51,6 +56,7 @@ public class AbsFieldValidatorsTest extends TestCase { assertFalse(field.getValidators().contains(validator2)); } + @Test public void testRemoveAllValidators() { field.addValidator(validator); field.addValidator(validator2); @@ -61,4 +67,51 @@ public class AbsFieldValidatorsTest extends TestCase { assertFalse(field.getValidators().contains(validator)); assertFalse(field.getValidators().contains(validator2)); } + + @Test + public void validatorShouldMakeImmediate() { + assertFalse("field should not be immediate by default", + field.isImmediate()); + field.addValidator(validator); + assertTrue("field should be immediate when it has a validator", + field.isImmediate()); + } + + @Test + public void nonImmediateFieldWithValidator() { + field.setImmediate(false); + field.addValidator(validator); + assertFalse("field should be non-immediate because explicitly set", + field.isImmediate()); + } + + @Test + public void removeValidatorMakesNonImmediate() { + field.addValidator(validator); + field.removeValidator(validator); + assertFalse( + "field should be non-immediate after validator was removed", + field.isImmediate()); + } + + @Test + public void requiredMakesImmediate() { + assertFalse("field should not be immediate by default", + field.isImmediate()); + field.setRequired(true); + assertTrue("field should be immediate when it is required", + field.isImmediate()); + } + + @Test + public void removeRequiredMakesNonImmediate() { + assertFalse("field should not be immediate by default", + field.isImmediate()); + field.setRequired(true); + field.setRequired(false); + assertFalse( + "field should not be immediate even though it was required", + field.isImmediate()); + } + } |