From 11ee7ffe05f2e815bb31f1fc62a4a3beda419ca9 Mon Sep 17 00:00:00 2001 From: Alexandre Chatiron Date: Tue, 13 Jan 2015 16:12:51 +0800 Subject: [PATCH] [#1861] feat(validation): add new methods insert, remove, hasError --- .../src/play/data/validation/Validation.java | 67 +++++++- .../play/data/validation/ValidationTest.java | 156 +++++++++++++++++- 2 files changed, 218 insertions(+), 5 deletions(-) diff --git a/framework/src/play/data/validation/Validation.java b/framework/src/play/data/validation/Validation.java index 7ca184730a..361935eea6 100644 --- a/framework/src/play/data/validation/Validation.java +++ b/framework/src/play/data/validation/Validation.java @@ -6,10 +6,12 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; + import net.sf.oval.configuration.annotation.AbstractAnnotationCheck; import play.Play; import play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer; @@ -70,10 +72,59 @@ public Map> errorsMap() { * @param variables Message variables */ public static void addError(String field, String message, String... variables) { - if (error(field) == null || !error(field).message.equals(message)) { - Validation.current().errors.add(new Error(field, message, variables)); + insertError(Validation.current().errors.size(), field, message, variables); + } + + /** + * Insert an error at the specified position in this list. + * @param index index at which the specified element is to be inserted + * @param field Field name + * @param message Message key + * @param variables Message variables + */ + public static void insertError(int index, String field, String message, String... variables) { + Error error = error(field); + if (error == null || !error.message.equals(message)) { + Validation.current().errors.add(index, new Error(field, message, variables)); } } + + /** + * Remove all errors on a field with the given message + * @param field Field name + * @param message Message key + */ + public static void removeErrors(String field, String message) { + Validation validation = current.get(); + if (validation != null) { + Iterator it = validation.errors.iterator(); + while (it.hasNext()) { + Error error = it.next(); + if (error.key != null && error.key.equals(field) && error.message.equals(message)) { + it.remove(); + } + } + } + } + + /** + * Remove all errors on a field + * @param field Field name + */ + public static void removeErrors(String field) { + Validation validation = current.get(); + if (validation != null) { + Iterator it = validation.errors.iterator(); + while (it.hasNext()) { + Error error = it.next(); + if (error.key != null && error.key.equals(field)) { + it.remove(); + } + } + } + } + + /** * @return True if the current request has errors @@ -83,6 +134,14 @@ public static boolean hasErrors() { return validation != null && validation.errors.size() > 0; } + /** + * @param field The field name + * @return true if field has some errors + */ + public static boolean hasErrors(String field){ + return error(field) != null; + } + /** * @param field The field name * @return First error related to this field @@ -135,7 +194,9 @@ public static boolean hasError(String field) { public static void clear() { current.get().errors.clear(); - ValidationPlugin.keys.get().clear(); + if(ValidationPlugin.keys.get() != null){ + ValidationPlugin.keys.get().clear(); + } } // ~~~~ Integration helper diff --git a/framework/test-src/play/data/validation/ValidationTest.java b/framework/test-src/play/data/validation/ValidationTest.java index 2bddabaab7..7d70003a0d 100644 --- a/framework/test-src/play/data/validation/ValidationTest.java +++ b/framework/test-src/play/data/validation/ValidationTest.java @@ -1,15 +1,15 @@ package play.data.validation; import org.junit.Test; -import play.i18n.MessagesBuilder; +import play.i18n.MessagesBuilder; import static org.fest.assertions.Assertions.assertThat; +import static org.junit.Assert.assertEquals; public class ValidationTest { @Test public void verifyError() { - new MessagesBuilder().build(); Validation.current.set(new Validation()); @@ -33,5 +33,157 @@ public void verifyError() { assertThat(Validation.errors(field)).containsOnly(Validation.error(field)); } + + @Test + public void addErrorTest(){ + new MessagesBuilder().build(); + + Validation.current.set(new Validation()); + + final String field = "f1"; + final String field2 = "f1.element"; + + final String errorMsg = "My errorMessage"; + + Validation.addError(field, errorMsg); + Validation.addError(field, errorMsg); + + Validation.addError(field2, errorMsg); + + assertThat( Validation.error(field).message).isEqualTo(errorMsg); + + // Test avoid insert duplicate message key + assertEquals(2, Validation.errors().size()); + + assertEquals(1, Validation.errors(field).size()); + assertEquals(1, Validation.errors(field2).size()); + + Validation.clear(); + + // Test clear empty the lsit + assertEquals(0, Validation.errors().size()); + assertEquals(0, Validation.errors(field).size()); + assertEquals(0, Validation.errors(field2).size()); + + final String errorMsgWithParam = "My errorMessage: %2$s"; + + Validation.addError(field, errorMsgWithParam, "param1"); + Validation.addError(field, errorMsgWithParam,"param2"); + + assertThat( Validation.error(field).message).isEqualTo(errorMsgWithParam); + + // Test avoid insert duplicate message key + assertEquals(1, Validation.errors().size()); + + assertEquals(1, Validation.errors(field).size()); + + assertEquals("My errorMessage: param1", Validation.error(field).message()); + } + + @Test + public void removeErrorTest(){ + new MessagesBuilder().build(); + + Validation.current.set(new Validation()); + + final String field = "f1"; + final String field2 = "f1.element"; + + final String errorMsg = "My errorMessage"; + final String errorMsg2 = "My errorMessage2"; + Validation.addError(field, errorMsg); + Validation.addError(field, errorMsg2); + + Validation.addError(field2, errorMsg); + Validation.addError(field2, errorMsg2); + + // Check the first error + assertThat( Validation.error(field).message).isEqualTo(errorMsg); + assertEquals(4, Validation.current().errors.size()); + + // Remove Errors on field2 + Validation.removeErrors(field2); + + assertEquals(2, Validation.errors().size()); + assertEquals(2, Validation.errors(field).size()); + assertEquals(0, Validation.errors(field2).size()); + + // Restore error on field2 + Validation.addError(field2, errorMsg); + Validation.addError(field2, errorMsg2); + + assertEquals(4, Validation.current().errors.size()); + + // Remove Errors on field + Validation.removeErrors(field); + + assertEquals(2, Validation.errors().size()); + assertEquals(0, Validation.errors(field).size()); + assertEquals(2, Validation.errors(field2).size()); + } + + @Test + public void removeErrorMessageTest(){ + new MessagesBuilder().build(); + + Validation.current.set(new Validation()); + + final String field = "f1"; + final String field2 = "f1.element"; + + final String errorMsg = "My errorMessage"; + final String errorMsg2 = "My errorMessage2"; + Validation.addError(field, errorMsg); + Validation.addError(field, errorMsg2); + + Validation.addError(field2, errorMsg); + Validation.addError(field2, errorMsg2); + + // Check the first error + assertThat( Validation.error(field).message).isEqualTo(errorMsg); + assertEquals(4, Validation.current().errors.size()); + + // Remove Errors on field2 + Validation.removeErrors(field2, errorMsg); + + assertEquals(3, Validation.errors().size()); + assertEquals(2, Validation.errors(field).size()); + assertEquals(1, Validation.errors(field2).size()); + + assertThat( Validation.error(field2).message).isEqualTo(errorMsg2); + + // Restore error on field2 + Validation.addError(field2, errorMsg); + Validation.addError(field2, errorMsg2); + + assertEquals(4, Validation.current().errors.size()); + + // Remove Errors on field + Validation.removeErrors(field, errorMsg); + + assertEquals(3, Validation.errors().size()); + assertEquals(1, Validation.errors(field).size()); + assertEquals(2, Validation.errors(field2).size()); + + assertThat( Validation.error(field).message).isEqualTo(errorMsg2); + } + + @Test + public void insertErrorTest(){ + new MessagesBuilder().build(); + + Validation.current.set(new Validation()); + final String field = "f1"; + + final String errorMsg = "My errorMessage"; + final String errorMsg2 = "My errorMessage2"; + Validation.addError(field, errorMsg); + Validation.insertError(0, field, errorMsg2); + + + // Check the first error + assertThat( Validation.error(field).message).isEqualTo(errorMsg2); + assertEquals(2, Validation.current().errors.size()); + } }