ActionValidator 类将验证至少其中一个字符串参数具有值,但并不是都具有值。您还将使 Value 参数成为必需参数,并且将编写 ActionValidator 类以使用星号来标记此参数。
if paramID = paramID of value parameter return true else return false使用以下示例源代码来覆盖 isParameterRequired 方法:
/( Mark the value parameter as required.*/
public boolean isParameterRequired(Action action, Parameter param)
{
//The value parameterID is 000
if(param.getParameterID().equals("000"))
{
return true;
}
return false;
}
Collect all the Parameter objects associated with the action Iterate through the Parameter objects and get the string1 and string2 Parameter objects Retrieve the string value of string1, check to see if it's null or has a length of 0 Retrieve the string value of string2, check to see if it's null or has a length of 0 Determine if both the parameters have values Return an error message Determine if neither of the parameters have values Return an informational message使用以下示例样本代码来覆盖此方法:
public ValidationResult validateAction(ActionValidationEvent event)
{
Parameter string1Param = null;
Parameter string2Param = null;
ValidationResult result = new ValidationResult();
//Iterate through and retrieve the parameter objects
for (Object o : event.action.getParameters())
{
if (((Parameter) o).getParameterId().equals("001"))
{
string1Param = (Parameter) o;
}
else if (((Parameter) o).getParameterId().equals("002"))
{
string2Param = (Parameter) o;
}
)
//Retrieve the parameter value for string1, and determine if there was input
String string1Val = (String) event.parameterValueMap.get(string1Param);
boolean string1IsThere = !(string1Val == null || string1Val.length() == 0);
//Retrieve the parameter value for string2, and determine if there was input
String string2Val = (String) event.parameterValueMap.get(string2Param);
boolean string2IsThere = !(string2Val == null || string2Val.length() == 0);
//Determine if both string1 and string2 are provided.
//Returns an error message.
if (string1IsThere && string2IsThere)
{
result.severity = ValidationResult.ERROR;
result.message = "Provide either String1 or String2.";
}
//Determine if neither string1 nor string2 is provided
//Returns an info message.
else if(!string1IsThere && !string2IsThere)
{
result.severity = ValidationResult.INFO;
result.message = "Enter a value for String1 or String2.";
}
return result;
}