htmlplayframeworkscala-template

How to get a value from a HTML text area in the Play Framework?


I am using scala templates within the Play Framework to create my views.

The user is required to enter some text into a text area. I would like to use this text to send into another view within my application.

<div class="ui form">
 <div class="field">
   <label>Please use the text box below</label>
     <textarea>//this is the text that i need to grab</textarea>
 </div>
    @pet_order(petId, //this is where i send in the text)
</div>

Can anyone give me some advice on how to achieve this?


Solution

  • The text area must be wrapped into a form and have the name attribute.

    Html will look like this:

      <form action="/some_path" method="post">
        <textarea name="attribute_name"></textarea>
        <input type="submit" value="Отправить">
      </form>
    

    You can use Play Framework's helpers to create a form in the view. Something like:

    @helper.form(action = routes.YourController.your_action) {
       @helper.textarea(myForm("attribute_name")) 
    }
    

    Read more here

    When you submit the form, the text from the text area will be sent to the server to a certain Controller#action. The url of the action is specified in the form's action attribute. The name of the parameter that will hold the entered text is specified in the textarea's name attribute.

    Then, in the action you have to extract the text from request attributes by it's name and send it to another view whether renderig the view and passing the text as a parameter or redirecting to another Controller#action passing the text as a parameter of the new request.

    You can use Play Framework's Form to extract request parameters. See the previous link.