You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2005/06/18 21:56:26 UTC
cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/valid IValidator.java
hlship 2005/06/18 12:56:26
Modified: framework/src/test/org/apache/tapestry/form/validator
TestRequired.java BaseValidatorTestCase.java
framework/src/java/org/apache/tapestry/form/validator
Required.java
framework/src/java/org/apache/tapestry/valid IValidator.java
Added: framework/src/test/org/apache/tapestry/form/validator
TestMaxLength.java TestMinDate.java
framework/src/java/org/apache/tapestry/form/validator
MaxLength.java MinDate.java
Log:
Add MaxLength and MinDate validators.
Revision Changes Path
1.2 +2 -4 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestRequired.java
Index: TestRequired.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestRequired.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestRequired.java 18 Jun 2005 18:09:14 -0000 1.1
+++ TestRequired.java 18 Jun 2005 19:56:26 -0000 1.2
@@ -72,7 +72,7 @@
{
IFormComponent field = newField("Fred");
ValidationMessages messages = newMessages(
- "Custom Message",
+ "custom",
ValidationStrings.REQUIRED_TEXT_FIELD,
new Object[]
{ "Fred" },
@@ -82,9 +82,7 @@
try
{
- Required required = new Required();
-
- required.setMessage("Custom Message");
+ Required required = new Required("message=custom");
required.validate(field, messages, null);
unreachable();
1.3 +6 -0 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/BaseValidatorTestCase.java
Index: BaseValidatorTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/BaseValidatorTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BaseValidatorTestCase.java 18 Jun 2005 18:09:14 -0000 1.2
+++ BaseValidatorTestCase.java 18 Jun 2005 19:56:26 -0000 1.3
@@ -15,6 +15,7 @@
package org.apache.tapestry.form.validator;
import org.apache.tapestry.components.BaseComponentTestCase;
+import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.easymock.MockControl;
@@ -65,4 +66,9 @@
control.setReturnValue(result);
}
+ protected FormComponentContributorContext newContext()
+ {
+ return (FormComponentContributorContext) newMock(FormComponentContributorContext.class);
+ }
+
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java
Index: TestMaxLength.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
import org.easymock.MockControl;
public class TestMaxLength extends BaseValidatorTestCase
{
public void testOK() throws Exception
{
IFormComponent field = newField();
ValidationMessages messages = newMessages();
String object = "short and sweet";
replayControls();
new MaxLength("maxLength=50").validate(field, messages, object);
verifyControls();
}
public void testFail()
{
IFormComponent field = newField("My Field");
ValidationMessages messages = newMessages(
null,
ValidationStrings.VALUE_TOO_LONG,
new Object[]
{ new Integer(10), "My Field" },
"Exception!");
replayControls();
try
{
new MaxLength("maxLength=10")
.validate(field, messages, "brevity is the essence of wit");
}
catch (ValidatorException ex)
{
assertEquals("Exception!", ex.getMessage());
assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
}
}
public void testFailCustomMessage()
{
IFormComponent field = newField("My Field");
ValidationMessages messages = newMessages(
"Too Long",
ValidationStrings.VALUE_TOO_LONG,
new Object[]
{ new Integer(10), "My Field" },
"Exception!");
replayControls();
try
{
new MaxLength("maxLength=10,message=Too Long").validate(
field,
messages,
"this should be more than ten characters");
}
catch (ValidatorException ex)
{
assertEquals("Exception!", ex.getMessage());
assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
}
}
public void testRenderContribution()
{
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
IFormComponent field = newField("My Field");
MockControl contextc = newControl(FormComponentContributorContext.class);
FormComponentContributorContext context = (FormComponentContributorContext) contextc
.getMock();
context.getFieldDOM();
contextc.setReturnValue("document.myform.myfield");
trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_LONG, new Object[]
{ new Integer(20), "My Field" }, "default message");
context
.addSubmitListener("function(event) { validate_max_length(event, document.myform.myfield, 20, 'default message'); }");
replayControls();
new MaxLength("maxLength=20").renderContribution(writer, cycle, context, field);
verifyControls();
}
}
1.1 jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java
Index: TestMinDate.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Date;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Tests for {@link org.apache.tapestry.form.validator.MinDate}.
*
* @author Howard Lewis Ship
* @since 4.0
*/
public class TestMinDate extends BaseValidatorTestCase
{
private static final long ONE_DAY = 24 * 60 * 60 * 1000l;
public void testOK() throws Exception
{
long now = System.currentTimeMillis();
Date today = new Date(now);
Date yesterday = new Date(now - ONE_DAY);
IFormComponent field = newField();
ValidationMessages message = newMessages();
replayControls();
MinDate v = new MinDate();
v.setMinDate(yesterday);
v.validate(field, message, today);
verifyControls();
}
public void testFail() throws Exception
{
long now = System.currentTimeMillis();
Date today = new Date(now);
Date tomorrow = new Date(now + ONE_DAY);
IFormComponent field = newField("Fred");
ValidationMessages message = newMessages(
null,
ValidationStrings.DATE_TOO_EARLY,
new Object[]
{ "Fred", tomorrow },
"default message");
replayControls();
MinDate v = new MinDate();
v.setMinDate(tomorrow);
try
{
v.validate(field, message, today);
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("default message", ex.getMessage());
assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
}
verifyControls();
}
public void testFailCustomMessage() throws Exception
{
long now = System.currentTimeMillis();
Date today = new Date(now);
Date tomorrow = new Date(now + ONE_DAY);
IFormComponent field = newField("Fred");
ValidationMessages message = newMessages(
"custom",
ValidationStrings.DATE_TOO_EARLY,
new Object[]
{ "Fred", tomorrow },
"custom message");
replayControls();
MinDate v = new MinDate("message=custom");
v.setMinDate(tomorrow);
try
{
v.validate(field, message, today);
unreachable();
}
catch (ValidatorException ex)
{
assertEquals("custom message", ex.getMessage());
assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
}
verifyControls();
}
public void testRenderComponentNoOp()
{
IMarkupWriter writer = newWriter();
IRequestCycle cycle = newCycle();
FormComponentContributorContext context = newContext();
IFormComponent field = newField();
replayControls();
new MinDate().renderContribution(writer, cycle, context, field);
verifyControls();
}
}
1.2 +10 -0 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/Required.java
Index: Required.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/Required.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Required.java 18 Jun 2005 18:09:14 -0000 1.1
+++ Required.java 18 Jun 2005 19:56:26 -0000 1.2
@@ -14,6 +14,7 @@
package org.apache.tapestry.form.validator;
+import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
@@ -33,6 +34,15 @@
{
private String _message;
+ public Required()
+ {
+ }
+
+ public Required(String initializer)
+ {
+ PropertyUtils.configureProperties(this, initializer);
+ }
+
public void setMessage(String message)
{
_message = message;
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java
Index: MaxLength.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Validator that ensures a string value does not exceed a maximum length.
*
* @author Howard Lewis Ship
* @since 4.0
*/
public class MaxLength implements Validator
{
private int _maxLength;
private String _message;
public MaxLength()
{
}
public MaxLength(String initializer)
{
PropertyUtils.configureProperties(this, initializer);
}
public void setMaxLength(int maxLength)
{
_maxLength = maxLength;
}
public void setMessage(String message)
{
_message = message;
}
public void validate(IFormComponent field, ValidationMessages messages, Object object)
throws ValidatorException
{
String string = (String) object;
if (string.length() > _maxLength)
throw new ValidatorException(buildMessage(messages, field),
ValidationConstraint.MAXIMUM_WIDTH);
}
protected String buildMessage(ValidationMessages messages, IFormComponent field)
{
return messages.formatValidationMessage(
_message,
ValidationStrings.VALUE_TOO_LONG,
new Object[]
{ new Integer(_maxLength), field.getDisplayName() });
}
public boolean getAcceptsNull()
{
return false;
}
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
FormComponentContributorContext context, IFormComponent field)
{
StringBuffer buffer = new StringBuffer("function(event) { validate_max_length(event, ");
buffer.append(context.getFieldDOM());
buffer.append(", ");
buffer.append(_maxLength);
buffer.append(", '");
buffer.append(buildMessage(context, field));
buffer.append("'); }");
context.addSubmitListener(buffer.toString());
}
}
1.1 jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/MinDate.java
Index: MinDate.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// 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.apache.tapestry.form.validator;
import java.util.Date;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;
/**
* Expects the value to be a {@link Date}, and constrains the date to follow a particular date.
*
* @author Howard M. Lewis Ship
* @since 4.0
*/
public class MinDate implements Validator
{
private Date _minDate;
private String _message;
public MinDate()
{
}
public MinDate(String initializer)
{
PropertyUtils.configureProperties(this, initializer);
}
public void setMessage(String message)
{
_message = message;
}
public void setMinDate(Date minDate)
{
_minDate = minDate;
}
public void validate(IFormComponent field, ValidationMessages messages, Object object)
throws ValidatorException
{
Date date = (Date) object;
if (date.before(_minDate))
throw new ValidatorException(buildMessage(messages, field),
ValidationConstraint.TOO_SMALL);
}
private String buildMessage(ValidationMessages messages, IFormComponent field)
{
return messages.formatValidationMessage(
_message,
ValidationStrings.DATE_TOO_EARLY,
new Object[]
{ field.getDisplayName(), _minDate });
}
public boolean getAcceptsNull()
{
return false;
}
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
FormComponentContributorContext context, IFormComponent field)
{
// No implementation yet; validation is only on the server side,
// for some reason.
}
}
1.4 +35 -39 jakarta-tapestry/framework/src/java/org/apache/tapestry/valid/IValidator.java
Index: IValidator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/valid/IValidator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- IValidator.java 6 Jan 2005 02:17:23 -0000 1.3
+++ IValidator.java 18 Jun 2005 19:56:26 -0000 1.4
@@ -19,60 +19,56 @@
import org.apache.tapestry.form.IFormComponent;
/**
- * An object that works with an {@link IFormComponent} to format output
- * (convert object values to strings values) and to process input
- * (convert strings to object values and validate them).
- *
- * @author Howard Lewis Ship
- * @since 1.0.8
- *
- **/
+ * An object that works with an {@link IFormComponent} to format output (convert object values to
+ * strings values) and to process input (convert strings to object values and validate them).
+ * <p>
+ * Note that this interface represents validation as supported in Tapestry 2.x to 3.0. It has been
+ * outdated (and will eventually be deprecated) by new support in Tapestry 4.0, centered around the
+ * {@link org.apache.tapestry.form.translator.Translator} and
+ * {@link org.apache.tapestry.form.validator.Validator} interfaces.
+ *
+ * @author Howard Lewis Ship
+ * @since 1.0.8
+ */
public interface IValidator
{
/**
- * All validators must implement a required property. If true,
- * the client must supply a non-null value.
- *
- **/
+ * All validators must implement a required property. If true, the client must supply a non-null
+ * value.
+ */
public boolean isRequired();
/**
- * Invoked during rendering to convert an object value (which may be null)
- * to a String. It is acceptible to return null. The string will be the
- * VALUE attribute of the HTML text field.
- *
- **/
+ * Invoked during rendering to convert an object value (which may be null) to a String. It is
+ * acceptible to return null. The string will be the VALUE attribute of the HTML text field.
+ */
public String toString(IFormComponent field, Object value);
/**
- * Converts input, submitted by the client, into an object value.
- * May return null if the input is null (and the required flag is false).
- *
- * <p>The input string will already have been trimmed. It may be null.
- *
- * @throws ValidatorException if the string cannot be converted into
- * an object, or the object is
- * not valid (due to other constraints).
- **/
+ * Converts input, submitted by the client, into an object value. May return null if the input
+ * is null (and the required flag is false).
+ * <p>
+ * The input string will already have been trimmed. It may be null.
+ *
+ * @throws ValidatorException
+ * if the string cannot be converted into an object, or the object is not valid (due
+ * to other constraints).
+ */
public Object toObject(IFormComponent field, String input) throws ValidatorException;
/**
- * Invoked by the field after it finishes rendering its tag (but before
- * the tag is closed) to allow the validator to provide a contribution to the
- * rendering process. Validators typically generated client-side JavaScript
- * to peform validation.
- *
- * @since 2.2
- *
- **/
-
- public void renderValidatorContribution(
- IFormComponent field,
- IMarkupWriter writer,
- IRequestCycle cycle);
+ * Invoked by the field after it finishes rendering its tag (but before the tag is closed) to
+ * allow the validator to provide a contribution to the rendering process. Validators typically
+ * generated client-side JavaScript to peform validation.
+ *
+ * @since 2.2
+ */
+
+ public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
+ IRequestCycle cycle);
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org