1. Create a Wicket Behavior that encapsulates your requirement.  Maybe
something like this:

public class DisableWhenFilledBehavior extends Behavior {

@Override
public void onConfigure(Component component) {
component.setEnabled(component.getDefaultModel() == null);
}

}

2. Add the behavior to all of the components in your form.  Something like
this:

Component myComp = new TextField<>("project", new
PropertyModel<Integer>(getModelObject(),
"project")).add(new DisableWhenFilledBehavior ());

3. After you handle the submission (e.g., persist the values to the db),
ask Wicket to re-render your form (or panel or page).  When Wicket
re-renders the form, it will re-execute
the DisableWhenFilledBehavior#onConfigure method against every component
that the behavior has been added to and, thus, recompute whether each
component is enabled or disabled.

form = new Form<>(...);
...
new AjaxButton(...) {

  public void onSubmit(AjaxRequestTarget target, Form<?> form) {
     // handle the values
     target.add(form);
  }

}

The other nice thing is that if the component is marked as disabled on the
server and the client, for whatever reason, submits a value, Wicket will
ignore it.  The framework is taking care of that for you; you don't need to
worry about it.

Thanks
Andrew

On Tue, Apr 19, 2016 at 1:28 PM, sub es <gsu...@gmail.com> wrote:

> In my opinion one should always validate inputs coming from a submit even
> if the inputs were disabled. Using tools like firebug, one can easily
> change values of disabled inputs and submit those changed values. Atleast I
> don't know about wicket not submitting disabled inputs, though I would not
> expect that behavior and would not rely on it.
>
> Best regards,
> Edwin
>
>   Ursprüngliche Nachricht
> Von:superbiss...@gmail.com
> Gesendet:19. April 2016 7:18 nachm.
> An:users@wicket.apache.org
> Antworten:users@wicket.apache.org
> Betreff:Component disabled if it contains data
>
> Hello,
> I'm using Wicket 6, with Java 1.7.
> I'm new to Wicket and I would like to know what is the best way to
> implement
> the following:
> -I have a webpage with several input fields bound to a db table, and a
> submit button.
> - two of the fields can be empty when submitting the changes to the db.
> - however, if the fields are filled once, after submitting the page, the
> user should not be allowed anymore to change the values(even if leaves and
> reenters the page for the same registrations)
>
> Could you please tell me what is the best way to achieve this?
>
> What I have done so far is:
> - on the page load, when the wicket components are added to the page, I
> check if the db fields have values, and if they do, then I set the
> components to disabled:
>
> Component myComp = new TextField<Integer>("project", new
> PropertyModel<Integer>(getModelObject(), "project"));
> if (getModelObject().getProject() != null)
> myComp.setEnabled(false);
> dtForm.add(myComp ).add(RangeValidator.<Integer>range(0, 999999999));
>
> - also on submit, I check if I have a value in my component and if yes, set
> the component to disable:
> if (getModelObject().getProject() != null ){
>      Component myComp  = get("project");
>      if (myComp.isEnabled()){
>             myComp.setEnabled(false);
>             dtForm.replace(tf);
>      }
> }
>
> what bothers me is that I need to do all the validation again in the submit
> button, every time the submit will be pressed.
>
> Is there a better way?
>
> Thank you
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Component-disabled-if-it-contains-data-tp4674313.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to