That was my plan exactly but I didn't know of a way to tell which button was 
doing the form submission.... until now!

Thanks for that Form#findSubmittingButton works beautifully!

I don't even think it's a hack as that validation should only really a problem 
if the user is submitting the form, not when they are merely adding items to 
the list.

We are using models for the storage of which items are in the list when the 
user clicks OK.
We are also using models to keep track of which items the user has selected 
(multiple selection is turned on).

The form is a classic 'tranfer' setup - with 2 list boxes and 2 buttons 
add/remove. The user can transfer items from one list to the other.

-----Original Message-----
From: Clint Checketts [mailto:checke...@gmail.com] 
Sent: Monday, 2 May 2011 3:40 PM
To: users@wicket.apache.org
Subject: Re: How to fail validation if ListMultipleChoice is empty

Good catch on AjaxSubmitButton being deprecated, I guess an IDe would have
made that obvious ;)

I do have to say using the getChoices over a proper model may give you more
work than needed in updating the underlying model objects (maybe consider
overriding the getConvertedInput to return getChoices)

It sounds like you have different buttons for adding and submitting. Correct
me if this is too hackish, but you could change the validator to check if
the addBtn is the submitting button, and ignore the validation in that case:

  form.add(new AbstractFormValidator()
  {
    public FormComponent[] getDependentFormComponents()
    {
      return null;
    }

    public void validate(Form<?> form)
    {
      List sets = targettedSetsList.getChoices();

      if ((*!addBtn.equals(form.findSubmittingButton()) &&* sets.size() == 0
) {
        targettedSetsList.error((IValidationError)new
  ValidationError().addMessageKey("error.noSetSpecified"));
      }
    }
  });


-Clint

On Mon, May 2, 2011 at 12:27 AM, Coleman, Chris <
chris.cole...@thalesgroup.com.au> wrote:

> According to the doco the default form processing behavior is executed for
> AjaxButton and AjaxSubmitButton (in fact AjaxSubmitButton appears to be
> deprecated - behaves the same as AjaxButton anyway?).
>
> I am using AjaxButton.
>
> I actually don't care about the selections but rather, the entries that the
> user has added to the list (whether selected or not) which is why I call
> getChoices() rather than getConvertedInput()
>
> The button code is:
>
>  AjaxButton addBtn = new AjaxButton("add") {
>    @Override
>    protected void onSubmit(AjaxRequestTarget target, Form form) {
>      update(target, selectedAvailableSets, availableSetsList,
> targettedSetsList);
>    }
>
>    @Override
>    protected void onError(AjaxRequestTarget target, Form form) {
>    }
>  };
>
>  addBtn.setOutputMarkupId(true);
>
>  add(addBtn);
>
>
> -----Original Message-----
> From: Clint Checketts [mailto:checke...@gmail.com]
> Sent: Monday, 2 May 2011 2:28 PM
> To: users@wicket.apache.org
> Subject: Re: How to fail validation if ListMultipleChoice is empty
>
> You are correct that the Form's validation should only fire when submitting
> the form. When an individual element is updated via ajax (as in an
> AjaxFormComponentUpdatingBehavior) then just the processing and validations
> steps are fired for the individual form component.
>
> It makes me wonder if you are using an AjaxSubmitButton instead of just an
> AjaxButton. (Or similarly an AjaxSubmitLink instead of an  AjaxLink) Mind
> including your button's code?
>
> Also, why are you calling getChoices() instead of getConvertedInput() in
> the
> validator? Choices represent the possible selection options, the converted
> input is the value of the selected choices.
>
> -Clint
>
> On Sun, May 1, 2011 at 9:25 PM, Coleman, Chris <
> chris.cole...@thalesgroup.com.au> wrote:
>
> > Yes, it's all via AJAX.
> >
> > In the last few minutes I've tried a different approach and it works ok
> but
> > it introduces another problem:
> >
> >  form.add(new AbstractFormValidator()
> >  {
> >    public FormComponent[] getDependentFormComponents()
> >    {
> >      return null;
> >    }
> >
> >    public void validate(Form<?> form)
> >    {
> >      List sets = targettedSetsList.getChoices();
> >
> >      if ( sets.size() == 0 ) {
> >        targettedSetsList.error((IValidationError)new
> > ValidationError().addMessageKey("error.noSetSpecified"));
> >      }
> >    }
> >  });
> >
> > This accurately detects when nothing is in the list and displays an error
> > message but once emptied we can not add new elements to the list because
> the
> > validation is also executed when the 'add' button is pressed. The
> validation
> > fails because the list is empty so the 'add' fails, making it impossible
> to
> > add new elements when the list is empty.
> >
> > I thought validation would only occur when the user submits the form but
> it
> > appears to be fired off whenever the user presses the 'add' button. Is
> this
> > to be expected?
> >
> >
> > -----Original Message-----
> > From: Clint Checketts [mailto:checke...@gmail.com]
> > Sent: Monday, 2 May 2011 12:10 PM
> > To: users@wicket.apache.org
> > Subject: Re: How to fail validation if ListMultipleChoice is empty
> >
> > Lets see the code about 'adding elements by pressing on a button'.  The
> > 'getValue()' method is returning the value from the list box's HTTP
> > submitted values, if the add button is submitting values via ajax or some
> > other means then it may need a different approach.
> >
> > -Clint
> >
> > On Sun, May 1, 2011 at 8:15 PM, Coleman, Chris <
> > chris.cole...@thalesgroup.com.au> wrote:
> >
> > > We have an app that allows people to add elements to a
> ListMultipleChoice
> > > by pressing on a button. We want the form to fail validation if the
> > > ListMultipleChoice contains no elements.
> > >
> > > I've tried this:
> > >
> > >  targettedSetsList.add(new IValidator()
> > >  {
> > >    public void validate(IValidatable validatable) {
> > >      // Always contains no items - strange
> > >      Collection list = (Collection)validatable.getValue();
> > >
> > >      if ( list.size() == 0 ) {
> > >        ValidationError ve = new ValidationError();
> > >        ve.setMessage("No sets have been specified for deployment");
> > >        validatable.error(ve);
> > >      }
> > >    }
> > >  });
> > >
> > > but at validation the list.size() is always 0 even if the user has
> added
> > > elements. Am I doing it the right way? Is there a better way?
> > >
> > >
> > >
> > >
> > >
> >
> DISCLAIMER:---------------------------------------------------------------------------
> > > This e-mail transmission and any documents, files and previous e-mail
> > > messages
> > > attached to it are private and confidential. They may contain
> proprietary
> > > or copyright
> > > material or information that is subject to legal professional
> privilege.
> > > They are for
> > > the use of the intended recipient only.  Any unauthorised viewing, use,
> > > disclosure,
> > > copying, alteration, storage or distribution of, or reliance on, this
> > > message is
> > > strictly prohibited. No part may be reproduced, adapted or transmitted
> > > without the
> > > written permission of the owner. If you have received this transmission
> > in
> > > error, or
> > > are not an authorised recipient, please immediately notify the sender
> by
> > > return email,
> > > delete this message and all copies from your e-mail system, and destroy
> > any
> > > printed
> > > copies. Receipt by anyone other than the intended recipient should not
> be
> > > deemed a
> > > waiver of any privilege or protection. Thales Australia does not
> warrant
> > or
> > > represent
> > > that this e-mail or any documents, files and previous e-mail messages
> > > attached are
> > > error or virus free.
> > >
> > >
> >
> --------------------------------------------------------------------------------------
> > >
> > >
> >
> >
> >
> >
> >
> DISCLAIMER:---------------------------------------------------------------------------
> > This e-mail transmission and any documents, files and previous e-mail
> > messages
> > attached to it are private and confidential. They may contain proprietary
> > or copyright
> > material or information that is subject to legal professional privilege.
> > They are for
> > the use of the intended recipient only.  Any unauthorised viewing, use,
> > disclosure,
> > copying, alteration, storage or distribution of, or reliance on, this
> > message is
> > strictly prohibited. No part may be reproduced, adapted or transmitted
> > without the
> > written permission of the owner. If you have received this transmission
> in
> > error, or
> > are not an authorised recipient, please immediately notify the sender by
> > return email,
> > delete this message and all copies from your e-mail system, and destroy
> any
> > printed
> > copies. Receipt by anyone other than the intended recipient should not be
> > deemed a
> > waiver of any privilege or protection. Thales Australia does not warrant
> or
> > represent
> > that this e-mail or any documents, files and previous e-mail messages
> > attached are
> > error or virus free.
> >
> >
> --------------------------------------------------------------------------------------
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> > For additional commands, e-mail: users-h...@wicket.apache.org
> >
> >
>
>
>
>
> DISCLAIMER:---------------------------------------------------------------------------
> This e-mail transmission and any documents, files and previous e-mail
> messages
> attached to it are private and confidential. They may contain proprietary
> or copyright
> material or information that is subject to legal professional privilege.
> They are for
> the use of the intended recipient only.  Any unauthorised viewing, use,
> disclosure,
> copying, alteration, storage or distribution of, or reliance on, this
> message is
> strictly prohibited. No part may be reproduced, adapted or transmitted
> without the
> written permission of the owner. If you have received this transmission in
> error, or
> are not an authorised recipient, please immediately notify the sender by
> return email,
> delete this message and all copies from your e-mail system, and destroy any
> printed
> copies. Receipt by anyone other than the intended recipient should not be
> deemed a
> waiver of any privilege or protection. Thales Australia does not warrant or
> represent
> that this e-mail or any documents, files and previous e-mail messages
> attached are
> error or virus free.
>
> --------------------------------------------------------------------------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>



DISCLAIMER:---------------------------------------------------------------------------
This e-mail transmission and any documents, files and previous e-mail messages
attached to it are private and confidential. They may contain proprietary or 
copyright
material or information that is subject to legal professional privilege. They 
are for
the use of the intended recipient only.  Any unauthorised viewing, use, 
disclosure,
copying, alteration, storage or distribution of, or reliance on, this message is
strictly prohibited. No part may be reproduced, adapted or transmitted without 
the
written permission of the owner. If you have received this transmission in 
error, or
are not an authorised recipient, please immediately notify the sender by return 
email,
delete this message and all copies from your e-mail system, and destroy any 
printed
copies. Receipt by anyone other than the intended recipient should not be 
deemed a
waiver of any privilege or protection. Thales Australia does not warrant or 
represent
that this e-mail or any documents, files and previous e-mail messages attached 
are
error or virus free.
--------------------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to