LTroya
LTroya

Reputation: 540

Play! forms always has errors

I am new in play! I am stuck with some forms that always has errors. I can't figure out what is the problem, even if all fields are filled.

routes

GET     /products/                  controllers.Products.list()
GET     /products/new               controllers.Products.newProduct()
POST    /products/                  controllers.Products.save()

Product's Controller.java

import play.data.Form;

private final static Form<Product> productForm = form(Product.class);

public static Result list() {
    List<Product> productList = Product.findAll();
    return ok(list.render(productList));
}

public static Result newProduct() {
    return ok(details.render(productForm));
}

public static Result save() {
    Form<Product> boundForm = productForm.bindFromRequest();
    if(boundForm.hasErrors()) {
        flash("error",
                "Please correct the form below.");
        return badRequest(details.render(boundForm));
    }

    // For mystery reasons, in this line, product is always null
    // Product product = boundForm.get();

    Product product = new Product();
    product.ean = boundForm.data().get("ean");
    product.name = boundForm.data().get("name");
    product.description = boundForm.data().get("description");
    product.save();

    flash("success",
            String.format("Successfully added product %s", product));

    return redirect(routes.Products.list());
}

Product's Model.java

import static play.data.validation.Constraints.Required;

public class Product {

    @Required
    public String ean;
    @Required
    public String name;
    public String description;
    ... 
}

Product's form.scala.html

@(productForm: Form[Product])


@main("Product form") {
    <h1>Product form</h1>
    @helper.form(action = routes.Products.save()) {
        <fieldset>
            <legend>Product (@productForm("name").valueOr("New"))</legend>

            @helper.inputText(productForm("ean"), '_label -> "EAN")
            @helper.inputText(productForm("name"),'_label -> "Name")

            @helper.textarea(productForm("description"), '_label -> "Description")

        </fieldset>
        <input type="submit" class="btn btn-primary" value="Save">
        <a class="btn" href="@routes.Products.list()">Cancel</a>
    }
}

Here is the debugger's screenshot, there is data and errors too :(

Using debugger

What am I doing wrong?

~~~~ Update ~~~~~

I added the list route and controller action

Here is the repo:

https://github.com/LTroya/up-and-running-play

Upvotes: 0

Views: 89

Answers (1)

Andrzej Jozwik
Andrzej Jozwik

Reputation: 14659

The solution - you need beans in java implementation (setters were missing):

public class Product {

    @Required
    public String ean;
    @Required
    public String name;
    public String description;
        public String getEan() {
        return ean;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void setEan(String ean) {
        this.ean = ean;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setName(String name) {
        this.name = name;
    } 
}

Upvotes: 1

Related Questions