IdentityValidator
-----------------
Key: TAPESTRY-410
URL: http://issues.apache.org/jira/browse/TAPESTRY-410
Project: Tapestry
Type: Improvement
Components: Framework
Versions: 4.0
Reporter: Ron Piterman
Priority: Minor
The following class compares the validated field against another field. It
works in two modes:
match=<fieldname> and differ=<fieldname>
Cheers,
Ron
package org.apache.tapestry.form.valid;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.TextField;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.validator.BaseValidator;
public class IdentityValidator extends BaseValidator {
private String _fieldName;
private int _matchType;
private String _identityMessage;
private static final int DIFFER = 0;
private static final int MATCH = 1;
public IdentityValidator() {
super();
}
public IdentityValidator(String initializer)
{
super(initializer);
}
public String toString(IFormComponent field, Object value)
{
if (value == null)
return null;
return value.toString();
}
public void validate(IFormComponent field, ValidationMessages messages,
Object object)
throws ValidatorException
{
TextField referent =
(TextField)field.getContainer().getComponent(_fieldName);
//TODO: if component is null treat _fieldName as an ognl
expression
boolean notEq = notEqual ( referent.getValue() , object );
if ( _matchType == MATCH ? notEq : !notEq )
throw new ValidatorException(buildIdentityMessage(messages,
field, referent),
ValidationConstraint.CONSISTENCY);
}
public String getMatch()
{
return _fieldName;
}
public void setMatch(String field)
{
_fieldName = field;
_matchType = MATCH;
}
public String getDiffer() {
return _fieldName;
}
public void setDiffer(String field) {
_fieldName = field;
_matchType = DIFFER;
}
/** @since 3.0 */
public String getIdentityMessage()
{
return _identityMessage;
}
/**
* Overrides the <code>field-too-short</code> bundle key. Parameter
{0} is the minimum length.
* Parameter {1} is the display name of the field.
*
* @since 3.0
*/
public void setMinimumLengthMessage(String string)
{
_identityMessage = string;
}
/** @since 3.0 */
protected String buildIdentityMessage(ValidationMessages messages,
IFormComponent field, IFormComponent referent)
{
Object[] parameters = new Object[] {
field.getDisplayName(), _matchType,
referent.getDisplayName()
};
return messages.formatValidationMessage(_identityMessage,
"invalid-field-equality", parameters);
}
private boolean notEqual(Object o1, Object o2) {
if (o1 == null && o2 == null)
return false;
if (o1 == null || o2 == null)
return true;
return !o1.equals(o2);
}
}
==============================
hivemind:
<contribution configuration-id="tapestry.form.validator.Validators">
<validator name="match" configurable="true"
class="org.apache.tapestry.form.valid.IdentityValidator"/>
</contribution>
<contribution configuration-id="tapestry.form.validator.Validators">
<validator name="differ" configurable="true"
class="org.apache.tapestry.form.valid.IdentityValidator"/>
</contribution>
=========================
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]