Reputation: 1000
I have never run into this before, so I am looking for help.
I have a simple one field form that has a Submit button which will not kick off the POST route for it. This code was working a few days ago, but now it just continually runs the GET route. There is no error, since it never makes it to the POST route and the function it is calling.
Here are the routes:
GET /createptp controllers.Application.ptpCreate()
POST /createptp controllers.Application.ptpSave()
Here is the form:
@(ptpForm: Form[Application.PTPForm], lookups: java.util.List[Lookup], users: java.util.List[User])
@main(null) {
<section id="ptpCreated">
<form class="formoid-solid-dark"
style="background-color: #FFFFFF; font-size: 14px; font-family: 'Trebuchet MS', 'Roboto', Arial, Helvetica, sans-serif; color: #34495E; max-width: 480px; min-width: 150px"
method="get" action="">
<div class="title">
<h3>Success</h3>
</div>
<div class="element-number" title="This is the eight-digit Provider ID number that you used for this setting in the portal maintained by Xerox. You may have had different Provider IDs for different services, settings, and/or waivers. Cannot be blank - numbers only">
<label class="title"><span class="required">* Legacy Provider Id...</span></label>
<div class="item-cont"><input type="text" name="legacy_Provider_Id" class='allow_numeric' required="required" min="1" max="99999999" maxlength="8" placeholder=""/><span class="icon-place"></span></div>
</div>
<div class="submit">
<input type="submit" value="Continue" />
</div>
<!-- This is needed for bottom shadow to appear... -->
<div></div>
</form>
</section>
}
Here are the functions in the controller:
public Result ptpCreate() {
// Set some needed values...
List<Lookup> lookups = Lookup.find.all();
List<User> users = User.find.all();
// Sort the lists...
AppGlobals sortThis = new AppGlobals();
Collections.sort(users, sortThis.new sortUsers());
Collections.sort(lookups, sortThis.new sortLookups());
return ok(adult1.render(form(PTPForm.class), lookups, users));
}
public Result ptpSave() {
Form<PTPForm> ptpEntry = form(PTPForm.class).bindFromRequest();
if (ptpEntry.hasErrors()) {
List<Lookup> lookups = Lookup.find.all();
List<User> users = User.find.all();
Logger.debug("Save ptp - errors");
// Sort the lists...
AppGlobals sortThis = new AppGlobals();
Collections.sort(users, sortThis.new sortUsers());
Collections.sort(lookups, sortThis.new sortLookups());
return badRequest(adult1.render(ptpEntry, lookups, users));
}
String userkey = AccessMiddleware.getSessionUserKey();
User user = null;
user = User.findByUserKey(userkey);
// Save the PTP record....
PTP ptp = new PTP();
PTPForm ptpForm = ptpEntry.get();
ptp.setLegacy_Provider_Id(ptpForm.legacy_Provider_Id);
ptp.save();
return ok(ptpcreated.render(ptp.getPtpkey()));
}
When I click the Continue button, it just goes back to the ptpCreate()
function and does not even bother with the POST route.
I have performed a clean
and compile
with no luck.
I have tried changing the <form action>
to action="@routes.Application.ptpSave()"
with no luck either.
I am completely lost on this one since there are no errors to track down.
Upvotes: 0
Views: 362
Reputation: 3251
As mentioned by @jrook, you need to make sure the <form>
tag uses a POST method. As shown in your question, your <form>
has method="get"
.
If you look in your routes, GET /createptp
goes to controllers.Application.ptpCreate()
whereas POST /createptp
goes to controllers.Application.ptpSave()
. They both have the same URL, so the method used to send the request is very important.
When you use action="@controllers.routes.Application.ptpSave()"
you've got the right URL, but you're still sending a GET request so it will go to controllers.Application.ptpCreate()
instead of controllers.Application.ptpSave()
.
The fix is to use method="POST"
to route to controllers.Application.ptpSave()
.
An alternative is to use a form template helper to render the correct method and action given a reverse route. E.g. @helper.form(action = controllers.routes.Application.ptpSave()) { ... }
. This will render <form method="GET" action="/createptp">...</form>
. See: https://www.playframework.com/documentation/2.6.x/JavaFormHelpers#Creating-a-%3Cform%3E-tag.
In the future to debug something like this use two techniques:
Use curl to manually send a POST request to /createptp
to check it works.
Use your browser's developer tools to test what kind of request you're sending when you submit a form.
I recently created a Play Framework issue to improve the documentation in this area: https://github.com/playframework/playframework/issues/8004. If you have an thoughts about this feel free to leave a comment.
Upvotes: 1