如果在提交表单时条件总是路由到hasError条件 [英] If condition always routing towards hasError condition while submitting a form

查看:187
本文介绍了如果在提交表单时条件总是路由到hasError条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个简单的表单来添加一个商店,每个商店都与成员 Shop.java模型 $ b

 包模型; 
@Entity
public class Shop扩展模型{

@ Id @ SequenceGenerator(name =shop_gen,sequenceName =shop_id_seq,allocationSize = 1)@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =shop_gen)@ Column(name =id)
public Long id;

@Required
public String name;

@需要
public String addressLine1;

public String addressLine2;

public String addressLine3;

@需要
public String city;

@必需
公共字符串城镇;

@需要
public String phoneNumber;

@ ManyToOne @ JoinColumn(name =email,
insertable = false,updatable = false,
nullable = false)@Required
public Member email;

public static Model.Finder<长,商店> find = new Model.Finder(Long.class,Shop.class);
public static Shop create(Shop shop){
shop.save();
退货店;
}

}

Member.java模型

$ b $ p $ @Entity
public class User extends Model {

@Id
@Email
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name =email)
public String email;

@需要
公共字符串密码;

@Required
public String firstName;

@必需的
公共字符串lastName;


$ b $ / code $ / pre
$ b

ShopController.java

  package controllers; 


公共类ShopController扩展控制器{
静态窗体< Shop> shopForm = Form.form(Shop.class);



public static Result submit()
{
Form< Shop> filledForm = shopForm.bindFromRequest();
if(filledForm.hasErrors()){
return badRequest(views.html.shop.create.render(filledForm,Member.names()));
}
else {
Shop shop = filledForm.get();
Shop.create(shop);

返回重定向(routes.ProductController.blank());



$ b $ / code>

我正在提交表单来添加一个商店它加载相同的页面条目填充意味着如果条件总是为真,我使用postgresql,这个问题没有发生在我以前的数据库的MySQL。



任何人都可以帮我吗?

解决方案

@Required $ shop.email 上的注释可能会触发验证错误,导致您的操作每次都返回一个HTTP 400(错误的请求)。我认为在调用 bindFromRequest 之后,该字段将是 null 。额外的日志记录或调试可以用来确认这一点。

即使传入的HTTP POST请求存储商店包含名称​​ email 的字段,Play也不知道如何转换该值(字符串)转换为成员对象。您必须从此字段删除验证,或者使用自定义数据联编程序告诉玩如何执行字符串到成员的转换。

作为一个额外的观察,我也会质疑您的其他表单类中的以下字段的声明:

/ p>

  @Id 
@Email
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name =email)
public String email;

OneToMany 注释表示这个实体有很多电子邮件地址。字段的类型(String)表示此实体的一个实例具有一个电子邮件地址。这两个陈述是矛盾的。


I have created a simple form to add a shop, every shop has a manytoone relation with the member Shop.java model

   package models;
   @Entity
   public class Shop extends Model {

    @Id@SequenceGenerator(name = "shop_gen", sequenceName = "shop_id_seq", allocationSize = 1)@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "shop_gen")@Column(name = "id")
    public Long id;

    @Required
    public String name;

    @Required
    public String addressLine1;

    public String addressLine2;

    public String addressLine3;

    @Required
    public String city;

    @Required
    public String town;

    @Required
    public String phoneNumber;

    @ManyToOne@JoinColumn(name = "email",
    insertable = false, updatable = false,
    nullable = false)@Required
    public Member email;

    public static Model.Finder < Long, Shop > find = new Model.Finder(Long.class, Shop.class);
    public static Shop create(Shop shop) {
        shop.save();
        return shop;
    }

  }

Member.java model

@Entity
public class User extends Model {

    @Id
    @Email
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "email")
    public String email;

    @Required
    public String password;

    @Required
    public String firstName;

    @Required
    public String lastName;


}

ShopController.java

package controllers;


public class ShopController extends Controller {
    static Form<Shop> shopForm = Form.form(Shop.class);



    public static Result submit()
    {
        Form<Shop> filledForm = shopForm.bindFromRequest();
        if(filledForm.hasErrors()) {
            return badRequest(views.html.shop.create.render(filledForm, Member.names()));
        }
        else {
            Shop shop = filledForm.get();
            Shop.create(shop);

            return redirect(routes.ProductController.blank());

        }
    }
}

but when I am submitting form to add a shop It loads the same page with the entry filled means if condition is always true ,I am using postgresql and this problem didn't occured in my previous database mysql.

Can anybody help me?

解决方案

The @Required annotation on Shop.email is probably triggering the validation error that is causing your action to return an HTTP 400 (bad request) each time. I think that the field will be null following your call to bindFromRequest. Extra logging or debugging can be used to confirm this.

Even if an incoming HTTP POST request to save a shop contains a field with name email, Play doesn't know how to turn the value of this field (a String) into a Member object. You have to either remove the validation from this field, or annotate it further with a custom data binder that tells Play how to perform the String to Member conversion.

As an extra observation, I would also question the declaration of the following field in your other form class:

@Id
@Email
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="email")
public String email;

The OneToMany annotation says that an instance of this entity has many email addresses. The type of the field (String) says that an instance of this entity has one email address. These two statements are contradictory.

这篇关于如果在提交表单时条件总是路由到hasError条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆