Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.component.validator;
- import java.io.Serializable;
- import java.util.List;
- import javax.faces.component.FacesComponent;
- import javax.faces.component.UIInput;
- import javax.faces.context.FacesContext;
- import org.omnifaces.component.validator.ValidateMultipleFields;
- @FacesComponent(MinMaxSelectionValidator.COMPONENT_TYPE)
- public class MinMaxSelectionValidator extends ValidateMultipleFields {
- public static final String COMPONENT_TYPE = "com.example.component.validator.MinMaxSelectionValidator";
- public static final String DEFAULT_MESSAGE = "You have to select between {0} and {1} values";
- private enum PropertyKeys {
- min, max
- }
- private static final int DEFAULT_MIN = 0;
- private static final int DEFAULT_MAX = -1;
- public MinMaxSelectionValidator() {
- super(DEFAULT_MESSAGE);
- }
- @Override
- protected boolean validateValues(FacesContext fc, List<UIInput> components,
- List<Object> values) {
- int countSelected = 0;
- if (values != null) {
- for (Object value : values) {
- if (value instanceof Boolean) {
- if (((Boolean)value) == true) {
- countSelected ++;
- }
- } else if (value instanceof String) {
- if (((String)value).equals("true")) {
- countSelected ++;
- }
- }
- }
- }
- if (countSelected < getMin() || countSelected > getMax()) {
- return false;
- }
- return true;
- }
- public int getMin() {
- return (Integer)getStateHelper().eval(PropertyKeys.min, DEFAULT_MIN);
- }
- public void setMin(int min) {
- getStateHelper().put(PropertyKeys.min, min);
- }
- public int getMax() {
- return (Integer)getStateHelper().eval(PropertyKeys.max, DEFAULT_MAX);
- }
- public void setMax(int max) {
- getStateHelper().put(PropertyKeys.max, max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement