Doobie2012
Doobie2012

Reputation: 66

Calling Static Method And Passing Model To Controller

I am just trying to pass my form to my controller and I'm getting this error no matter what I try:

render(play.api.data.Form<models.Service>) in 'null' cannot be applied to (play.data.Form<models.Service)

Error Line:

return ok(info.render(sServiceForm));

Info.scale.html - View

@(serviceForm : Form[Service])
@import helper._

@main("Service info") {
  <h1>Service Information</h1>
  @helper.form(action = routes.Services.save()) {
    <fieldset>
      <legend>Service</legend>
      @helper.inputText(serviceForm.field("code"), '_label -> "Code")
      @helper.inputText(serviceForm.field("description"), '_label -> "Description")
      @helper.inputText(serviceForm.field("description"), '_label -> "Description")
    </fieldset>
  <input type="submit" value="Save" />
  }
}

Service.java - Model

package models;

import com.avaje.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * Created by James on 3/4/2016.
 */
// Telling play framework that this is a class thats going to map as a model to save service records
@Entity
public class Service extends Model {
    // Internal ID to reference a certain activity
    @Id
    public String code;
    public String description;
}

Services.java - Controller

package controllers;

import models.Service;
import play.mvc.Controller;
import play.mvc.Result;
import play.data.Form;
import views.html.services.info;

/**
 * Created by James on 3/4/2016.
 */
public class Services extends Controller {
    // Creating static class variable, calling static method and passing our model class.
    //private static final Form<Service> sServiceForm = Form.form(Service.class);
   private static final Form<Service> sServiceForm = play.data.Form.form(Service.class);
    public Result list() {
        return TODO;
    }

    public Result addService() {
        return ok(info.render(sServiceForm));
    }

    public Result save()
    {
        return TODO;
    }
}

If I comment out:

private static final Form<Service> sServiceForm = Form.form(Service.class);

And change my addService to return TODO; the site compiles fine and I can go throughout it fine. This line breaks the site even if I am still returning TODO:

private static final Form<Service> sServiceForm = Form.form(Service.class);

Upvotes: 1

Views: 1220

Answers (1)

fvthree
fvthree

Reputation: 51

In case you want it to work using FormFactory. You can follow this code. Someone from gitter helped me. So, credits for him actually.

Here's my code:

Services.java

package controllers;

import models.Service;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.services.info;

import javax.inject.Inject;

public class Services extends Controller {
    private final Form<Service> serviceForm;

    @Inject
    public Services(FormFactory formFactory) {
        this.serviceForm =  formFactory.form(Service.class);
    }

    public Result list() {
        return TODO;
    }

    public Result addService() {
        return ok(info.render(serviceForm));
    }

    public Result save() {
        return TODO;
    }
}

and info.scala.html

@(serviceForm : play.data.Form[Service])
@import helper._
@main("Service info"){

  <h1>Service Information</h1>

  @form(action = routes.Services.save()) {

    <fieldset>

      <legend>Service</legend>
      @inputText(serviceForm("code"), '_label -> "Code")
      @inputText(serviceForm("description"), '_label -> "Description")
      @inputText(serviceForm("description"), '_label -> "Description")

    </fieldset>

    <input type="submit" value="Save"/>

  }

}

Upvotes: 1

Related Questions