Skip to content

Commit

Permalink
[#1861] feat(validation): add new methods insert, remove, hasError
Browse files Browse the repository at this point in the history
  • Loading branch information
xael-fry committed Jan 13, 2015
1 parent 7f9c8d8 commit 11ee7ff
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 5 deletions.
67 changes: 64 additions & 3 deletions framework/src/play/data/validation/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,10 +72,59 @@ public Map<String, List<Error>> 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<Error> 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<Error> 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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
156 changes: 154 additions & 2 deletions framework/test-src/play/data/validation/ValidationTest.java
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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());
}
}

0 comments on commit 11ee7ff

Please sign in to comment.