Issue with bindFromRequest in Play! Framework 2.3

前端 未结 6 1621
夕颜
夕颜 2021-01-12 01:37

I\'m trying to use the automatic binding feature of Play, without success. I\'m developing in Java, on Eclipse 4.4 Luna.

Here is my form :

6条回答
  •  清歌不尽
    2021-01-12 02:05

    There is absolutely no link between the binding and your database. Do not follow @blackbishop's advice telling you to change all the fields of your model to String. That's a very bad idea, if there are different types, there is a reason...

    Moreover, Ebean (supposing you're using it) or JPA generate database column types according to your Java properties type. Why would you store a 0 or a 1 in a varchar column ?

    Follow these rules :

    1. Use a singular name for your models (User instead of Users)
    2. Always use camel case for your properties, no underscores (firstName instead of first_name, lastName instead of last_name...)
    3. Check errors before getting the value after binding

    That should give you this :

    public static Result createUser() {
        Form form = Form.form(User.class).bindFromRequest();
        if (form.hasErrors()) {
            // Do what you have to do (i.e. : redirect to the form with a flash message)
        }
        User u = form.get();
        u.save();
        return redirect(routes.Backend.allUsers());
    }
    

    By the way, in your testing lines, the user_id is correctly generated because you have no validation rules and this data comes from the database, not the form.

提交回复
热议问题