1

I'm trying to update an old Play 2 Framework app to last version 2.6.19

One view uses Post Query Ajax to send Forms to controller but now after update the code the data binded in Form is always empty ("none").

If I use the documentation and write a ScalaForm, the data arrives but I'm not able to know If I can mix scripts with Scala Form Helpers because I need to perfom some actions before and after submit it.

I suspect that maybe the problem are filters or something in application.conf and can not bind the data successfully.

I'm using jquery 1.11.2

This is the code that I use in the old version but now is not working.

The view:

<form method="post" id="entrarLTerme">
<h4>Término Actual:</h4>
<p class="redex" contenteditable="true" id="ent" spellcheck="false"></p>
<button id="accepta" type="submit" value="val">@messages.messages("tilcgfs.entrar")</button>
</form>

<script>
    $("#entrarLTerme").submit(function (e) {
                uncheck();
                var formURL = $(this).attr("action");
                var aux = $("#ent").text();
                $.ajax(
                        {
                            url: formURL,
                            type: "POST",
                            data: {valor: aux, op: "0", pag: "2"},
                            success: function (data) {
                                // Call some JS functions
                                }
                            },
                            error: function () {
                                // Call some JS functions
                            }
                        });
                e.preventDefault();
            });
</script>

And the Controller:

class TilcWT @Inject()(component: ControllerComponents, instanciesTilcWT: InstanciesTilcWT,langs: Langs) extends AbstractController(component) with I18nSupport {

  implicit var messages: Messages = MessagesImpl(Lang("ca"), messagesApi)

  val opcionsDefinicions = Form(
    tuple(
      "valor" -> text,
      "op" -> text,
      "pag" -> text))

  def opcions = Action { implicit request =>
    val usuari: String = request.session.get("user").get
    opcionsDefinicions.bindFromRequest.fold(
      formWithErrors => {
        BadRequest("Not Alloweddd")
      },
      options => {
        val valor = options._1
        val opt = options._2
        val page = options._3
        BadRequest("Not Alloweddd")
      }
    )
    BadRequest("Not Allowed")
  }

}

Before I use the fold function I "get" this without any problem. The debugger arrives correcty to the "opcions" method and of course the BadRequest are dummy.

My application.conf looks like this:

# The application languages
# ~~~~~
play.i18n.langs=["en","ca","es"]

play.filters.enabled=[]

Thanks in advance

1 Answers1

0

I had issues with posts in Play 2.6, due to this CSRF protection thing.

To check if your issue is related to it, first try to deactivate this CSRF filter like advised here: How can I disable the CSRF filter on Play 2.6?

Then of course you must use Play helpers to add CSRF to your Post and URL requests, for example like:

@form(routes.ItemsController.save()) {
    @CSRF.formField
    . ..
}
J. Chomel
  • 8,193
  • 15
  • 41
  • 69