wicket-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Bors <p...@bors.ws>
Subject Re: Adding Custom validator for Listview having multiple checkboxes
Date Wed, 15 May 2013 17:06:53 GMT
Place your table inside a CheckGroup and add the validator there.

Below is my implementation of this validator:

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
public class AtLeastOneSelectedValidator<T> implements
IValidator<Collection<T>> {
    private static final long serialVersionUID = 1L;

    private String resourceKey;

 public AtLeastOneSelectedValidator(String resourceKey) {
  setResourceKey(resourceKey);
 }

 @Override
    public void validate(IValidatable<Collection<T>> validatable) {
        int numSelected = validatable.getValue().size();
        if(numSelected < 1) {
            validatable.error(new
ValidationError().addKey(getResourceKey()));
        }
    }

    public void setResourceKey(String resourceKey) {
        this.resourceKey = resourceKey;
    }

    public String getResourceKey() {
        return resourceKey;
    }
}


On Wed, May 15, 2013 at 11:28 AM, Saravanan <saravanan.d1@tcs.com> wrote:

> I have Listview with multiple checkboxes. I need to do validation that
> atleast one checkbox is selected.
>
> For this I have used Custom validator to validate checkboxes and throw
> error. This validator has list having checkboxes
>
> In Listview populateItem method I am adding the checkboxes to this
> validator
> list. But issue is how to add validator to form.
> I used checkbox.getform().add(validator); But it is adding for every
> checkbox in listview. So on running application validate method of
> validator
> is called for each checkbox.
> How to resolve ths. Kindly provide input
>
> #########################################
> new ListView<T>("repeater", listModel) {
>
>
>             @Override
>             protected void populateItem(final ListItem<T> item) {
>                 final T current = item.getModelObject();
>
>                 final IModel<Boolean> cbModel = new
> ListChoiceCheckBoxModel<T>(current, binding) {
>                     @Override
>                     protected boolean isChoiceEnabled(final T choice) {
>                         return
> EnumCheckBoxRepeaterPanel.this.isChoiceEnabled(choice);
>                     }
>                 };
>                 CheckBox chkbox = new AjaxCheckBox("checkbox", cbModel) {
>                     @Override
>                     protected void onConfigure() {
>                         setEnabled(isChoiceEnabled(current));
>                     }
>                 };
>
>                 item.add(chkbox);
>                 validator.addComponents(chkbox);
>            chkbox.getForm().add(new CheckboxesSelectValidator(chkBoxList,
> "Invalid"));
>                 item.add(new Label("label", localizedModel(labels,
> current)));
>             }
>         }.setReuseItems(true);
>
> #########################################
> public class CheckboxesSelectValidator extends AbstractFormValidator {
>
>     private static final long serialVersionUID = -5475763159946590330L;
>     /** form components to be checked. */
>     // private final CheckBox[] components;
>     private final String optionsMessage;
>     private final List<CheckBox> components;
>
>     public CheckboxesSelectValidator(final List<CheckBox> components, final
> String optionsMessage) {
>         this.components = components;
>         this.optionsMessage = optionsMessage;
>     }
>
>     public void addComponents(final CheckBox checkbox) {
>         components.add(checkbox);
>     }
>
>     @Override
>     public FormComponent[] getDependentFormComponents() {
>         return components.toArray(new FormComponent[components.size()]);
>     }
>
>     @Override
>     public void validate(final Form<?> form) {
>         for (CheckBox component : components) {
>             component.isEnabled();
>             component.getValue();
>         }
>
>     }
>
> }
>
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Adding-Custom-validator-for-Listview-having-multiple-checkboxes-tp4658825.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message