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.
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.
@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.