Nikolay Matkheev Nikolay Matkheev - 17 days ago 9
Java Question

Play Framework - create form without a model

Is it possible to create a form without model in Play Framework 2.5 ?

I tried to add code in controller:

package forms;

import play.data.validation.Constraints;
import play.data.Form;


public class UserForm extends Form{
@Constraints.Required
@Constraints.Email
public String email;
}


And then in Controller:

public Result cabinet() {
Form<UserForm> userform = formFactory.form(UserForm.class);
return ok(payment.render(userform)); }


Finally, in view:

@import forms.UserForm
@(form: Form[UserForm])

@import b3.vertical.fieldConstructor // Declares a vertical field constructor as default

@cabinet() {
@b3.form(routes.HomeController.landing) {
@b3.email( form("email"), '_label -> "Email", 'placeholder -> "example@mail.com" )
@b3.submit('class -> "btn btn-default"){ Sign in }
}
}


When I moved to the test url, I've got a null exception error:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.NullPointerException]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: java.lang.NullPointerException: null
at controllers.HomeController.cabinet(HomeController.java:38)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$5$$anonfun$apply$5.apply(Routes.scala:199)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$5$$anonfun$apply$5.apply(Routes.scala:199)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:156)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:136)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:73)
at play.http.HttpRequestHandler$1.call(HttpRequestHandler.java:54)
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)


How to do creation properly?

UPDATE
The problem was in FormFactory - this should be explicitly defined:

@Inject
public HomeController(FormFactory formFactory) {
this.formFactory = formFactory;
}


And this thing solve the NullException trouble

Answer

It actually depends on the form, what type of form?

  1. Do you want to validate something from your database?
  2. You need model when you want to check for values in databases etc
  3. Model is where the business logic is validated or executed and returned back to the controller in MVC.

For a simple form, just to test some sample username and password you don't need model, you can get them done using controllers and views in the framework!!

Please follow this video, In this video the author uses Users.java in models. This is the best way, kind of how things work when you wanna build a big website.

However, in your case, you can implement this functionality in controllers using DynamicForm, please check this post for more details.