/**
* This field size must be lower than.
* Message key: validation.maxSize
* $1: field name
* $2: reference value
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(checkWith = MaxSizeCheck.class)
public @interface MaxSize {
String message() default MaxSizeCheck.mes;
int value();
}
I created a new validation annotation for checking the size of a file that may be useful. It is used like this:
{{{#!java
@FileMaxSi ze(Config. PROFILE_ MAX_SIZE_ BYTES) File picture) {
public static void updatePicture(Long userId,
...
}
}}}
The code for the annotation and supporting check class is:
{{{#!java validation;
package play.data.
import java.lang. annotation. ElementType; annotation. Retention; annotation. RetentionPolicy ; annotation. Target; oval.configurat ion.annotation. Constraint;
import java.lang.
import java.lang.
import java.lang.
import net.sf.
/** RetentionPolicy .RUNTIME) {ElementType. FIELD, ElementType. PARAMETER} ) checkWith = MaxSizeCheck.class)
* This field size must be lower than.
* Message key: validation.maxSize
* $1: field name
* $2: reference value
*/
@Retention(
@Target(
@Constraint(
public @interface MaxSize {
String message() default MaxSizeCheck.mes;
int value();
}
package play.data. validation;
import java.util.HashMap; oval.Validator; oval.configurat ion.annotation. AbstractAnnotat ionCheck; oval.context. OValContext;
import java.util.Map;
import net.sf.
import net.sf.
import net.sf.
public class MaxSizeCheck extends AbstractAnnotat ionCheck< MaxSize> {
final static String mes = "validation. maxSize" ;
int maxSize;
@Override
this.maxSize = annotation.value();
setMessage( annotation. message( ));
public void configure(MaxSize annotation) {
}
public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
requireMessage VariablesRecrea tion(); ).length( ) == 0) { ).length( ) <= maxSize;
if (value == null || value.toString(
return true;
}
return value.toString(
}
@Override riables( ) {
messageVariabl es.put( "maxSize" , Integer. toString( maxSize) );
public Map<String, String> createMessageVa
Map<String, String> messageVariables = new HashMap();
return messageVariables;
}
}
}}}