[Java] Play2 form validations not working

85 views
Skip to first unread message

Gocool Dev

unread,
Feb 10, 2015, 4:50:07 AM2/10/15
to play-fr...@googlegroups.com
Hi,

 I am trying to have a simple form validation with Play2 Java flavoured project and my form validations doesn't seem to work.The code is as follows,

My controller has something like below,

public static Result newProduct() {

Form<Product> filledForm = Form.form(Product.class).bindFromRequest();

if (productForm.hasErrors()) {

return badRequest(views.html.index.render(Product.all(), filledForm));

} else {

Product.create(filledForm.get()); //Getting IllegalStateException: No value here

return redirect(routes.Application.products());

}

}

And my model looks like below,


public class Product {

private static final long serialVersionUID = 94587092799205246L;

@Id

public Long id;

@Required

public String title;

@Required

public Pricing pricing;

public List<ValidationError> validate(){

List<ValidationError> errors = new ArrayList<ValidationError>();

if(id == null){

errors.add(new ValidationError("id", "Id is null or empty"));

}

if(title == null){

errors.add(new ValidationError("title", "Title of the product is null or empty"));

}

return errors.isEmpty() ? null : errors;

}

public static void create(Product product) {

product.id = Long.valueOf(Product.products.count()+1);

Product.products.save(product);

}

}


And my form looks like the below,

@(productForm : Form[Product])

@import helper._

@import helpers.BootstrapHelper._

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">

<h2>Add new product</h2>

@main("Add"){

@form(action = routes.Application.newProduct()){

@inputText(productForm("title"),'_label -> "Title")

@inputText(productForm("pricing.cost"),'_label -> "Cost")

@inputText(productForm("pricing.price"),'_label -> "Price")

@inputText(productForm("pricing.promoPrice"),'_label -> "Promo Price")

@inputText(productForm("pricing.savings"),'_label -> "Savings")

@checkbox(productForm("pricing.onSale"),'_label -> "On Sale")

<input type="submit" class="btn btn-default" value="Add"/>

}

}


I get an exception saying "IllegalStateException: No value" in the comment specified above. I am not sure what I am missing, I know I am not adding the errors in the html page but still the validations should be preventing a submit which isn't working. Any pointers on what I am doing wrong would be great. Thanks for your time.

Regards,

G


Re.


Lucas Arenas

unread,
Feb 10, 2015, 7:09:38 AM2/10/15
to play-fr...@googlegroups.com

Hello, you should change the next line:

if (productForm.hasErrors()) {

for

if (filledForm.hasErrors()) {

Because the form is being saved in that variable:

Form<Product> filledForm = Form.form(Product.class).bindFromRequest();

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gocool Dev

unread,
Feb 11, 2015, 8:58:22 AM2/11/15
to play-fr...@googlegroups.com
Thanks a bunch Lucas, yes that was the issue.

On a related note, I am having a issue where the errors displayed in the html pages are showing up as just keys -

eg:
error.invalid 
error.invalid 


and I tried using @Required(message="error.price.invalid") and adding a messages.en in conf folder for the key error.price.invalid. But it still keeps showing up the default key "error.invalid". I would be glad if it at least shows the message but just shows up the key.

My html page has following code to display the errors,

@if(productForm.hasErrors) {

    <div class="alert alert-error">

        @if(productForm.errors.size() > 0) {

            @for((key, value) <- productForm.errors) {

                    @for(err <- value) {

                        @err.message().toString() <br/>

                    }

            }

        } 

    </div>

}


--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/8PVWpmmDCAQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages