Hi,
I created a small validator and would like to share it with you all.
If there is already something like that, I will appreciate if someone direct
me to it.
This is a From Validator.
If you have several checkboxes in a form and you want that *at least* one
checkbox to be selected, you can use this validator.
Here is the code:
public class CheckboxesSelectValidator extends AbstractFormValidator {
private static final long serialVersionUID = -5475763159946590330L;
/** form components to be checked. */
private final CheckBox[] components;
private final String optionsMessage;
public CheckboxesSelectValidator(CheckBox[] components, String
optionsMessage) {
this.components = components;
this.optionsMessage = optionsMessage;
}
public FormComponent[] getDependentFormComponents() {
return components;
}
/**
* At least one check box should be checked
*/
public void validate(Form form) {
for (CheckBox component : components) {
if (((Boolean) component.getConvertedInput()).booleanValue()) {
return;
}
}
error(components[0]);
}
@SuppressWarnings("unchecked")
@Override
protected Map variablesMap() {
Map args = new HashMap(1);
args.put("options", optionsMessage);
return args;
}
}
Here is a sample usage:
masterConfCheckbox.getForm().add(
new CheckboxesSelectValidator(new CheckBox[] {
masterConfCheckbox,
modelconfCheckbox },
getString("Reports.Parameters.validator.configuration")));
And this is the error message (taken from the properties file):
CheckboxesSelectValidator = Please select at least one option from
${options}.
Any remarks will be appreciated as well.
Eyal Golan
[email protected]
Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74
P Save a tree. Please don't print this e-mail unless it's really necessary