Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a NotEmptyValidator for Binder setRequired functionality.
Change-Id: I703912f29d884b655bafa7e801bf1454b65e32b4
- Loading branch information
Denis Anisimov
committed
Aug 15, 2016
1 parent
3612112
commit 06414d6
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters