pmichna
pmichna

Reputation: 4888

How to pass a value from a form to a POST request in Play Framework?

An excerpt from routes:

GET     /scenarios/:scenario/checkpoints/add        controllers.CheckpointController.createCheckpointGET(scenario: Long)
POST    /checkpoints/add                            controllers.CheckpointController.createCheckpointPOST(scneario: Long)

An excerpt from the view:

@(form: Form[CheckpointController.Creation], user: User, scenario: Scenario)
[...]
@helper.form(routes.CheckpointController.createCheckpointPOST) {
    <input id="name" type="text" name="name" placeholder="Checkpoint name" value="@form("name").value">
    <input id="longitudeDegrees" type="number" name="longitudeDegrees" placeholder="Longitude degrees" value="@form("longitudeDegrees").value">   
    <input id="longitudeMinutes" type="number" name="longitudeMinutes" placeholder="Longitude minutes" value="@form("longitudeMinutes").value">
    <input id="latitudeDegrees" type="number" name="latitudeDegrees" placeholder="Latitude degrees" value="@form("latitudeDegrees").value">
    <input id="latitudeMinutes" type="number" name="latitudeMinutes" placeholder="Latitude minutes" value="@form("latitudeMinutes").value">
    <input id="message" type="text" name="message" placeholder="Message to send"  value="@form("message").value">
    <input id="points" type="number" name="points" placeholder="Points" value="@form("points").value">

    <button type="submit">
        Create
    </button>
}

How do I pass scenario.id to the createCheckpointPOST(scenario: Long)? I know I can send it via hidden input but is it possible to do it other way?

Upvotes: 0

Views: 134

Answers (1)

Isammoc
Isammoc

Reputation: 893

The way that seems the most natural to me, the url contains the scenario.id :

in routes :

POST    /checkpoints/:scenario/add                            controllers.CheckpointController.createCheckpointPOST(scenario: Long)

in your view :

@helper.form(routes.CheckpointController.createCheckpointPOST(scenario.id)) {

Upvotes: 1

Related Questions