Method value in class Field is deprecated - ? - I cannot see it in Javadocs

Hi,
I want to upgrade my Java application to Play 2.6.12. In the form edit page I have got following code:

    <legend>Student ( @studentForm("name").value() )</legend>
    <input type="hidden" value="@studentForm("id").value()" name="id" />

to display name of the object and pass the DB object ID.
SBT shell during compilation of this code complains:

[warn] C:\dev\app1\app\views\details.scala.html:10:44: method value in class Field is deprecated: see corresponding Javadoc for more information.
[warn]     <legend>Student ( @studentForm("name").value() )</legend>

I have browsed the Javadocs: https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.data.Form@value:Option[T] however value() does not appear to be deprecated.

Should I bother with this warning or just ignore it?
Is it possible to use method vauleOrElse here in any way?

BTW: I consider passing DB id to client side a bit dangerous as it can be modified. Is it possible to pass identity of the existing Ebeans object to be updated in the form in more secure manner?

Best regards,
Wojtek

You are looking at the wrong javadocs (the scala api one).
You said you are using Java so you have to use the Play-Java javadocs:
https://www.playframework.com/documentation/2.6.x/api/java/play/data/Form.Field.html#value--

Regards,
Matthias

As far as I know Play Framework uses Scala in HTML templates.

But I have also tried your solution before: I replaced value() with getValue(), but it did not work as intended. In IntelliJ getValue was displayed in red, and in the browser the attribute value contained: Optional[Tom] instead of Tom.

What’s the full type of studentForm("name")? Is it a play.data.Field or a play.api.data.Field? The former is the Java API, that latter is the Scala API. Even though Twirl templates use Scala syntax they can still use Java objects.

I was sure it is a Scala class, as I imported Scala class:

// Top of the template
@(studentForm: Form[Student])
@import helper._
@import play.api.data.Form

The import comes after the template parameters, where Form is used, so the Form you’re referring to in the parameters is likely not the same Form you imported. If you are in a Play Java project, play.data.Form (the Java version) should be in the default template imports. Also, if you’re able to call getValue() to get an Optional (a Java API type) then it must be a Java form.

I think you’re doing it right with getValue() but you just need to unwrap the Optional. You probably want to call .getValue().orElse("default value"), with some default.

Also, IntelliJ could be wrong for any number of reasons; maybe you didn’t refresh your SBT project or maybe it just assumes the wrong default template imports.

2 Likes

Thank you @mkurz and @richdougherty, you were right, but @ Greg (sorry, as a new user I could not mention more than two users) directed me to the solution:
I moved imports above templeate header, but it did not resolve problem. Then I imported whole package @ play.api.data._ and then the solution become obvious:

[error] D:\dev\play-lecture\edukacja2-v1\app\views\details.scala.html:4:16: reference to Form is ambiguous;
[error] it is imported twice in the same scope by
[error] import play.api.data._
[error] and import play.data._
[error] @(studentForm: Form[Student])

So the Java play.data.Form is imported automatically by template parameter.
As I switched to @ import play.data._ everything works fine and even IntelliJ makes right suggestions.

1 Like