Prevent submitting browser form when Wicket AjaxFormValidatingBehaviour validation fails

I have a page with the Wizard component. The user can navigate through the wizard panels using the next and previous buttons, on which I have full (non-ajax) forms, so that the application is friendly.

When I click the next button, I would like to try to do an ajax form validation (if javascript is enabled). I tried:

nextButton.add( new AjaxFormValidatingBehavior( form, "onsubmit") ); 

to add such a check. The behavior works, however, when validation errors occur, the browser still submits the entire form.

What is a Wicket to prevent the browser from submitting a form in this case?

+4
source share
2 answers

Override the onError() method either in the form or in AjaxFormValidatingBehavior . If you do this by behavior, I'm not sure if this will prevent the form from being submitted or not.

 new AjaxFormValidatingBehavior( form, "onsubmit") { public void onSubmit() {} public void onError() {} } 
0
source

Maybe a little late, but here is the answer:

 public class SomePage extends WebPage { private FeedbackPanel feedbackMessageError = new FeedbackPanel("feedbackTabAddEmpMesError", new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR)); public SomePage(String id) { final Form<Void> form = new Form<>("tabFormAddEmp"); add(form); //Name textfield cannot be empty final FormComponent<String> tabAddEmpName = new RequiredTextField<>("tabAddEmpName", Model.of("")); tabAddEmpName.setLabel(Model.of("Name")); tabAddEmpName.setOutputMarkupId(true); //Salarynumber has to be minimal 10 char long final FormComponent<String> tabAddEmpLoon = new RequiredTextField<>("tabAddEmpLoon", Model.of("")); tabAddEmpLoon.add(new StringValidator(10, null)).setLabel(Model.of("Salarynumber")); tabAddEmpLoon.setOutputMarkupId(true); final Button button = new Button("tabFormAddEmpBut"); form.add(tabAddEmpName , tabAddEmpLoon, button); button.add(new AjaxFormValidatingBehavior(form, "onclick") { @Override public void onError(AjaxRequestTarget target) { //Add feedbackpanel to your html and voila! target.add(feedbackMessageError); } @Override protected void onSubmit(AjaxRequestTarget target) { //Do some logic over here } } } } 
0
source

Source: https://habr.com/ru/post/1307500/


All Articles