com.opensymphony.xwork2.validator.validators
public class: RegexFieldValidator [javadoc |
source]
java.lang.Object
com.opensymphony.xwork2.validator.validators.ValidatorSupport
com.opensymphony.xwork2.validator.validators.FieldValidatorSupport
com.opensymphony.xwork2.validator.validators.RegexFieldValidator
All Implemented Interfaces:
FieldValidator, ShortCircuitableValidator, Validator
Direct Known Subclasses:
EmailValidator
Validates a string field using a regular expression.
- fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
- expression - The RegExp expression REQUIRED
- caseSensitive - Boolean (Optional). Sets whether the expression should be matched against in a case-sensitive way. Default is
true.
- trim - Boolean (Optional). Sets whether the expression should be trimed before matching. Default is
true.
<validators>
<!-- Plain Validator Syntax -->
<validator type="regex">
<param name="fieldName">myStrangePostcode</param>
<param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]<>/param>
</validator>
<!-- Field Validator Syntax -->
<field name="myStrangePostcode">
<field-validator type="regex">
<param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]></param>
</field-validator>
</field>
</validators>
- author:
Quake - Wang
- version:
$ - Date: 2007-03-31 23:48:53 +0200 (Sat, 31 Mar 2007) $ $Revision: 1425 $
| Methods from com.opensymphony.xwork2.validator.validators.ValidatorSupport: |
|---|
|
addActionError, addFieldError, conditionalParse, getDefaultMessage, getFieldValue, getMessage, getMessageKey, getParse, getValidatorContext, getValidatorType, isShortCircuit, setDefaultMessage, setMessageKey, setParse, setShortCircuit, setValidatorContext, setValidatorType, setValueStack |
| Method from com.opensymphony.xwork2.validator.validators.RegexFieldValidator Detail: |
public String getExpression() {
return expression;
}
|
public boolean isCaseSensitive() {
return caseSensitive;
}
|
public boolean isTrimed() {
return trim;
}
|
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
Sets whether the expression should be matched against in
a case-sensitive way. Default is true. |
public void setExpression(String expression) {
this.expression = expression;
}
Sets the regular expression to be matched. |
public void setTrim(boolean trim) {
this.trim = trim;
}
Sets whether the expression should be trimed before matching.
Default is true. |
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
Object value = this.getFieldValue(fieldName, object);
// if there is no value - don't do comparison
// if a value is required, a required validator should be added to the field
if (value == null || expression == null) {
return;
}
// XW-375 - must be a string
if (!(value instanceof String)) {
return;
}
// string must not be empty
String str = ((String) value).trim();
if (str.length() == 0) {
return;
}
// match against expression
Pattern pattern;
if (isCaseSensitive()) {
pattern = Pattern.compile(expression);
} else {
pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
}
String compare = (String) value;
if ( trim ) {
compare = compare.trim();
}
Matcher matcher = pattern.matcher( compare );
if (!matcher.matches()) {
addFieldError(fieldName, object);
}
}
|