]> source.dussan.org Git - vaadin-framework.git/commitdiff
Provide a NotEmptyValidator for Binder setRequired functionality.
authorDenis Anisimov <denis@vaadin.com>
Mon, 15 Aug 2016 14:01:27 +0000 (17:01 +0300)
committerDenis Anisimov <denis@vaadin.com>
Mon, 15 Aug 2016 14:01:27 +0000 (17:01 +0300)
Change-Id: I703912f29d884b655bafa7e801bf1454b65e32b4

server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java [new file with mode: 0644]
server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/data/validator/NullValidatorTest.java [deleted file]

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 (file)
index 0000000..ac827c4
--- /dev/null
@@ -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 (file)
index 0000000..78101b8
--- /dev/null
@@ -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/NotNullValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java
new file mode 100644 (file)
index 0000000..8762e74
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+public class NotNullValidatorTest {
+
+    @Test
+    public void nullValueIsDisallowed() {
+        NotNullValidator validator = new NotNullValidator("foo");
+        Result<String> result = validator.apply(null);
+        Assert.assertTrue(result.isError());
+        Assert.assertEquals("foo", result.getMessage().get());
+    }
+
+    @Test
+    public void nonNullValueIsAllowed() {
+        NotNullValidator validator = new NotNullValidator("foo");
+        Result<String> result = validator.apply("bar");
+        Assert.assertFalse(result.isError());
+        result.ifOk(value -> Assert.assertEquals("bar", value));
+        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/NullValidatorTest.java
deleted file mode 100644 (file)
index 26c5105..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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;
-
-public class NullValidatorTest {
-
-    @Test
-    public void nullValueIsDisallowed() {
-        NotNullValidator validator = new NotNullValidator("foo");
-        Result<String> result = validator.apply(null);
-        Assert.assertTrue(result.isError());
-        Assert.assertEquals("foo", result.getMessage().get());
-    }
-
-    @Test
-    public void nonNullValueIsAllowed() {
-        NotNullValidator validator = new NotNullValidator("foo");
-        Result<String> result = validator.apply("bar");
-        Assert.assertFalse(result.isError());
-        result.ifOk(value -> Assert.assertEquals("bar", value));
-        result.ifError(msg -> Assert.fail());
-    }
-
-}