3

I'm trying to customize the Play framework -Computer database with Java example. However, I can't seem to get the submit button on the "edit" page to work(annotated). Here's the form:

@(id: Long, computerForm: Form[Provider])

@import helper._

@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.render) } 

@main {

    <h1>Provide fb*emphasized text*</h1>

    @form(routes.Application.update(id)) {

        <fieldset>

            @inputText(computerForm("fb"), '_label -> "Feedback", '_help -> "")
             @inputText(computerForm("rating"), '_label -> "Rating", '_help -> "")
        </fieldset>  
        <div class="actions">
            <!-- This button does nothing!-->
            <input type="submit" value="Submit fb!" class="btn primary"> or 
            <a href="@routes.Application.list()" class="btn">Cancel</a> 
        </div>

    }    
}

And here's the update method from Application.java:

public static Result update(Long id) {
    Form<Provider> computerForm = form(Provider.class).bindFromRequest();
    if(computerForm.hasErrors()) {
        return badRequest(editForm.render(id, computerForm));
    }
    computerForm.get().update(id);
    flash("success", "Provider " + computerForm.get().fullname + " has been updated");
    return GO_HOME;
}

What am I doing wrong?

EDIT- More details.

Model: Provider.java

/**
 * Company entity managed by Ebean
 */
@Entity
public class Provider extends Model {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;

@Constraints.Required
public String fullname;

public String tech;

public String experience;

public String email;

public String feedback;
public int rating;
/**
 * Generic query helper for entity Provider with id Long
 */
public static Model.Finder<Long, Provider> find = new Model.Finder<Long, Provider>(Long.class, Provider.class);

public static Map<String, String> options() {
    LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
    for (Provider c : Provider.find.orderBy("tech").findList()) {
        options.put(c.id.toString(), c.fullname);
    }
    return options;
}

public static Page<Provider> page(int page, int pageSize, String sortBy, String order, String filter) {
    return find.where().ilike(sortBy, "%" + filter + "%").findPagingList(pageSize).setFetchAhead(false)
            .getPage(page);
}

}

Question 2: What happens when I click the button : Nothing visible. But when I look real close, it seems to redirect to the same page (the update page). But based on the controller I'd expect it to go back back to the home page based on the GO_HOME variable.

Application.java

package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;

import views.html.*;

import models.*;

/**
 * Manage a database of Providers
 */
public class Application extends Controller {

    /**
     * This result directly redirect to application home.
     */
    public static Result GO_HOME = redirect(
        routes.Application.list(0, "fullname", "asc", "")
    );

    /**
     * Handle default path requests, redirect to computers list
     */
    public static Result index() {
        return GO_HOME;
    }

    /**
     * Display the paginated list of Providers.
     * 
     * @param page Current page number (starts from 0)
     * @param sortBy Column to be sorted
     * @param order Sort order (either asc or desc)
     * @param filter Filter applied on computer names
     */
    public static Result list(int page, String sortBy, String order, String filter) {
        return ok(
            list.render( 
                Provider.page(page, 10, sortBy, order, filter),
                sortBy, order, filter
            )
        );
    }

    /**
     * Display the 'edit form' of a existing Computer.
     *
     * @param id Id of the computer to edit
     * // TODO : Fix wiring and button actions 
     */
    public static Result edit(Long id) {
        Form<Provider> computerForm = form(Provider.class).fill(
            Provider.find.byId(id)
        );
        return ok(
            editForm.render(id, computerForm)
        );
    }

    /**
     * Handle the 'edit form' submission 
     *
     * @param id Id of the computer to edit
     */
    public static Result update(Long id) {
        Form<Provider> computerForm = form(Provider.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(editForm.render(id, computerForm));
        }
        computerForm.get().update(id);
        flash("success", "Computer " + computerForm.get().fullname + " has been updated");
        return GO_HOME;
    }

    /**
     * Display the 'new Provider form'.
     */
    public static Result create() {
        Form<Provider> computerForm = form(Provider.class);
        return ok(
            createForm.render(computerForm)
        );
    }

    /**
     * Handle the 'new Provider form' submission 
     */
    public static Result save() {
        Form<Provider> computerForm = form(Provider.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(createForm.render(computerForm));
        }
        computerForm.get().save();
        flash("success", "Provider " + computerForm.get().fullname + " has been created");
        return GO_HOME;
    }

}
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
NewbUser
  • 303
  • 2
  • 12
  • This code looks functional, nothing obviously wrong - could you update your post with the Form Model and what happens when you click submit? Your code seems slightly different than the code I found in the link. – tgk Apr 29 '16 at 05:47
  • What do you mean by "not working"? It generates an error? Nothing happens? The new data is not persisted? Please edit your post to add more details about what you expected to happen and what is actually happening. – marcospereira Apr 29 '16 at 05:59
  • @marcospereira,@user3384225 .. it seems to redirect to the same page instead of persisting the new data and redirecting to the list page. I have added the controller and model as well to give a better picture. Thanks! – NewbUser Apr 29 '16 at 17:36
  • It is returning to the same page with which http status? – marcospereira Apr 29 '16 at 17:49
  • Yes it is returning to the same page. How can I find out the http status? EDIT: I dont see a visible 500 error or anything .. but Im not sure if that's just the framework swallowing up the error though. – NewbUser Apr 29 '16 at 22:05
  • I'd guess it's returning status 400? You can view status code as documented here: http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome – tgk May 01 '16 at 06:06

1 Answers1

1

Forgive me, I'm most familiar with Scala but my suspicion is that you are failing form validation on submit and its returning the same page:

if(computerForm.hasErrors()) {
  return badRequest(createForm.render(computerForm));
}

In your Provider class looks like you are defining a constraint for fullname:

@Constraints.Required
public String fullname;

But you're not including fullname in your form. Could you remove the constraint from the model or add the field to your form?

Also unrelated to your failure, you are referencing form field fb in the view, but the Provider model doesn't have that value? It should be feedback?

tgk
  • 3,857
  • 2
  • 27
  • 42