user3248346
user3248346

Reputation:

Bypass Form Validation

I have a form with a radio button group for which I have set the validation logic that one of the radio buttons in the group must be selected on submission.

However I also have a cancel button on that form which I have wrapped as a hyperlink as follows:

<a href="@routes.MyController.index">
    <button>Cancel</button>
</a>

The issue is that when the user clicks this cancel button when no radio buttons have been selected (because they want to leave the form for some reason) , the validation logic kicks in prompting the user to select a radio button option.

How do I bypass the validation when the cancel button is clicked?

Upvotes: 1

Views: 617

Answers (1)

Andriy Kuba
Andriy Kuba

Reputation: 8263

Change button to div:

<a href="@routes.MyController.index">
  <div>Cancel</div>
</a>

The default type attribute is submit so it sends the form to the server and validates it. In your case the button with the default submit attribute catch the click event, so a even not hits.

If you need the button tag then you need to override its behavior with the javascript like:

<button type="button" onclick="window.location.href='@routes.MyController.index'">Cancel</button>

P.S.

It's have nothing to do with playframework, it's just an HTML behavior.

Upvotes: 1

Related Questions