initial validation constraint spi commit
This commit is contained in:
parent
70ee067400
commit
2bc76bcd68
|
|
@ -13,14 +13,8 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ui.validation;
|
||||
|
||||
/**
|
||||
* TODO Document ValidateResults.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface ValidationResults {
|
||||
public interface ValidationResults extends Iterable<ValidationResult> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,25 +13,10 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ui.validation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* TODO Document Validator.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface Validator {
|
||||
|
||||
/**
|
||||
* TODO Document validate().
|
||||
*
|
||||
* @param model
|
||||
* @param properties
|
||||
* @return
|
||||
*/
|
||||
ValidationResults validate(Object model, List<String> properties);
|
||||
|
||||
public interface Validator<M> {
|
||||
ValidationResults validate(M model, List<String> properties);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright 2004-2009 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.ui.validation.constraint;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.ui.alert.Severity;
|
||||
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Impact {
|
||||
|
||||
Severity value();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright 2004-2009 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.ui.validation.constraint;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Message {
|
||||
|
||||
String[] value();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright 2004-2009 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.ui.validation.constraint;
|
||||
|
||||
public interface ValidationConstraint<T> {
|
||||
boolean validate(T value);
|
||||
}
|
||||
|
|
@ -3,13 +3,17 @@ package org.springframework.ui.lifecycle;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.ui.alert.Alert;
|
||||
import org.springframework.ui.alert.Alerts;
|
||||
import org.springframework.ui.alert.Severity;
|
||||
import org.springframework.ui.alert.support.DefaultAlertContext;
|
||||
import org.springframework.ui.binding.Binder;
|
||||
|
|
@ -17,6 +21,12 @@ import org.springframework.ui.binding.Bound;
|
|||
import org.springframework.ui.binding.Model;
|
||||
import org.springframework.ui.binding.support.WebBinderFactory;
|
||||
import org.springframework.ui.format.number.CurrencyFormat;
|
||||
import org.springframework.ui.validation.ValidationResult;
|
||||
import org.springframework.ui.validation.ValidationResults;
|
||||
import org.springframework.ui.validation.Validator;
|
||||
import org.springframework.ui.validation.constraint.Impact;
|
||||
import org.springframework.ui.validation.constraint.Message;
|
||||
import org.springframework.ui.validation.constraint.ValidationConstraint;
|
||||
|
||||
public class BindAndValidateLifecycleTests {
|
||||
|
||||
|
|
@ -31,7 +41,112 @@ public class BindAndValidateLifecycleTests {
|
|||
model = new TestBean();
|
||||
alertContext = new DefaultAlertContext();
|
||||
Binder binder = new WebBinderFactory().getBinder(model);
|
||||
lifecycle = new BindAndValidateLifecycle(binder, null, alertContext);
|
||||
Validator<TestBean> validator = new TestBeanValidator();
|
||||
lifecycle = new BindAndValidateLifecycle(binder, validator, alertContext);
|
||||
}
|
||||
|
||||
static class TestBeanValidator implements Validator<TestBean> {
|
||||
public ValidationResults validate(TestBean model, List<String> properties) {
|
||||
TestValidationResults results = new TestValidationResults();
|
||||
RequiredConstraint required = new RequiredConstraint();
|
||||
boolean valid = required.validate(model.getString());
|
||||
if (!valid) {
|
||||
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
@Message({"en=#{label} is required", "es=#{label} es necesario"})
|
||||
static class RequiredConstraint implements ValidationConstraint<Object> {
|
||||
public boolean validate(Object value) {
|
||||
if (value != null) {
|
||||
return value instanceof String ? ((String) value).length() > 0 : true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Message("#{label} is a weak password")
|
||||
@Impact(Severity.WARNING)
|
||||
static class StrongPasswordConstraint implements ValidationConstraint<String> {
|
||||
public boolean validate(String password) {
|
||||
if (password.length() > 6) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Message("#{label} could not be confirmed; #{value} must match #{model.confirmPassword}")
|
||||
static class ConfirmedPasswordConstraint implements ValidationConstraint<SignupForm> {
|
||||
public boolean validate(SignupForm form) {
|
||||
if (form.password.equals(form.confirmPassword)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Message("#{label} must be between #{this.min} and #{this.max}")
|
||||
static class RangeConstraint implements ValidationConstraint<Number> {
|
||||
private Long min;
|
||||
|
||||
private Long max;
|
||||
|
||||
public RangeConstraint(Long min, Long max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public boolean validate(Number value) {
|
||||
Long longValue = value.longValue();
|
||||
if (longValue >= min && longValue <= max) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class TestValidationResults implements ValidationResults {
|
||||
private List<ValidationResult> results = new ArrayList<ValidationResult>();
|
||||
|
||||
public void add(ValidationResult result) {
|
||||
results.add(result);
|
||||
}
|
||||
|
||||
public Iterator<ValidationResult> iterator() {
|
||||
return results.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestValidationFailure implements ValidationResult {
|
||||
|
||||
private String property;
|
||||
|
||||
private String message;
|
||||
|
||||
public TestValidationFailure(String property, String message) {
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
public String getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public Alert getAlert() {
|
||||
return Alerts.error(message);
|
||||
}
|
||||
|
||||
public boolean isFailure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -200,7 +315,12 @@ public class BindAndValidateLifecycleTests {
|
|||
public void setNotEditable(String notEditable) {
|
||||
this.notEditable = notEditable;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class SignupForm {
|
||||
private String username;
|
||||
private String password;
|
||||
private String confirmPassword;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue