summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Anisimov <denis@vaadin.com>2016-08-15 17:01:27 +0300
committerDenis Anisimov <denis@vaadin.com>2016-08-15 17:01:27 +0300
commit06414d6f47be9a5f9df58a1a8705352b84a4e368 (patch)
tree66f42b0e0836ece1398af68135dd2136e6dedc7f
parent36121120bb066393ca0a3edd8adfda66c0e0250f (diff)
downloadvaadin-framework-06414d6f47be9a5f9df58a1a8705352b84a4e368.tar.gz
vaadin-framework-06414d6f47be9a5f9df58a1a8705352b84a4e368.zip
Provide a NotEmptyValidator for Binder setRequired functionality.
Change-Id: I703912f29d884b655bafa7e801bf1454b65e32b4
-rw-r--r--server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java76
-rw-r--r--server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java54
-rw-r--r--server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java (renamed from server/src/test/java/com/vaadin/data/validator/NullValidatorTest.java)2
3 files changed, 131 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
new file mode 100644
index 0000000000..ac827c45b6
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import java.util.Objects;
+
+import com.vaadin.data.HasRequired;
+import com.vaadin.data.Result;
+import com.vaadin.data.Validator;
+
+/**
+ * Simple validator to check against {@code null} value and empty {@link String}
+ * value.
+ * <p>
+ * This validator works similar to {@link NotNullValidator} but in addition it
+ * also check whether the value is not an empty String.
+ * <p>
+ * The main purpose of this validator is its usage with {@link HasRequired}
+ * field instances.
+ * <p>
+ * If the field is required, it is visually indicated in the user interface.
+ * Furthermore, required fields requires "non-empty" validator. So in addition
+ * to call {@link HasRequired#setRequired(boolean)} method one should add an
+ * instance of this validator explicitly so the code looks like this:
+ *
+ * <pre>
+ * <code>
+ * Binder<Bean,String, String> binder = new Binder<>();
+ * TextField name = new TextField();
+ * name.setRequired(true);
+ * binder.forField(name).withValidator(
+ * new NonEmptyValidator("Name cannot be empty"))
+ * .bind(Bean::getName, Bean::setName);
+ * </code>
+ * </pre>
+ *
+ * @see HasRequired
+ * @author Vaadin Ltd
+ * @since 8.0
+ *
+ */
+public class NotEmptyValidator<T> implements Validator<T> {
+
+ private final String message;
+
+ /**
+ * @param message
+ * error validation message
+ */
+ public NotEmptyValidator(String message) {
+ this.message = message;
+ }
+
+ @Override
+ public Result<T> apply(T value) {
+ if (Objects.isNull(value) || Objects.equals(value, "")) {
+ return Result.error(message);
+ } else {
+ return Result.ok(value);
+ }
+ }
+
+}
diff --git a/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java
new file mode 100644
index 0000000000..78101b898e
--- /dev/null
+++ b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.validator;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.data.Result;
+
+/**
+ * @author Vaadin Ltd
+ *
+ */
+public class NotEmptyValidatorTest {
+
+ @Test
+ public void nullValueIsDisallowed() {
+ NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
+ Result<String> result = validator.apply(null);
+ Assert.assertTrue(result.isError());
+ Assert.assertEquals("foo", result.getMessage().get());
+ }
+
+ @Test
+ public void emptyValueIsDisallowed() {
+ NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
+ Result<String> result = validator.apply("");
+ Assert.assertTrue(result.isError());
+ Assert.assertEquals("foo", result.getMessage().get());
+ }
+
+ @Test
+ public void nonNullValueIsAllowed() {
+ NotEmptyValidator<Object> validator = new NotEmptyValidator<>("foo");
+ Object value = new Object();
+ Result<Object> result = validator.apply(value);
+ Assert.assertFalse(result.isError());
+ result.ifOk(val -> Assert.assertEquals(value, val));
+ result.ifError(msg -> Assert.fail());
+ }
+}
diff --git a/server/src/test/java/com/vaadin/data/validator/NullValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java
index 26c510523c..8762e74fa3 100644
--- a/server/src/test/java/com/vaadin/data/validator/NullValidatorTest.java
+++ b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java
@@ -20,7 +20,7 @@ import org.junit.Test;
import com.vaadin.data.Result;
-public class NullValidatorTest {
+public class NotNullValidatorTest {
@Test
public void nullValueIsDisallowed() {