java - Play-framework 2 Validation -
java - Play-framework 2 Validation -
so did 1 line tweak on play-computer-database-java activator template
the models:
computer:
package models; import java.util.*; import javax.persistence.*; import play.db.ebean.*; import play.data.format.*; import play.data.validation.*; import com.avaje.ebean.*; /** * computer entity managed ebean */ @entity public class computer extends model { private static final long serialversionuid = 1l; @id public long id; @constraints.required public string name; @formats.datetime(pattern = "yyyy-mm-dd") public date introduced; @formats.datetime(pattern = "yyyy-mm-dd") public date discontinued; @constraints.required @manytoone public company company; /** * generic query helper entity computer id long */ public static finder<long, computer> find = new finder<long, computer>(long.class, computer.class); /** * homecoming page of computer * * @param page page display * @param pagesize number of computers per page * @param sortby computer property used sorting * @param order sort order (either or asc or desc) * @param filter filter applied on name column */ public static page<computer> page(int page, int pagesize, string sortby, string order, string filter) { homecoming find.where() .ilike("name", "%" + filter + "%") .orderby(sortby + " " + order) .fetch("company") .findpaginglist(pagesize) .setfetchahead(false) .getpage(page); } }
company:
package models; import java.util.*; import javax.persistence.*; import play.db.ebean.*; import play.data.validation.*; /** * company entity managed ebean */ @entity public class company extends model { private static final long serialversionuid = 1l; @id public long id; @constraints.required public string name; /** * generic query helper entity company id long */ public static model.finder<long, company> find = new model.finder<long, company>(long.class, company.class); public static map<string, string> options() { linkedhashmap<string, string> options = new linkedhashmap<string, string>(); (company c : company.find.orderby("name").findlist()) { options.put(c.id.tostring(), c.name); } homecoming options; } }
and controller
package controllers; import play.mvc.*; import play.data.*; import static play.data.form.*; import views.html.*; import models.*; /** * manage database of computers */ public class application extends controller { /** * result straight redirect application home. */ public static result go_home = redirect( routes.application.list(0, "name", "asc", "") ); /** * handle default path requests, redirect computers list */ public static result index() { homecoming go_home; } /** * display paginated list of computers. * * @param page current page number (starts 0) * @param sortby column sorted * @param order sort order (either asc or desc) * @param filter filter applied on computer names */ public static result list(int page, string sortby, string order, string filter) { homecoming ok( list.render( computer.page(page, 10, sortby, order, filter), sortby, order, filter ) ); } /** * display 'edit form' of existing computer. * * @param id id of computer edit */ public static result edit(long id) { form<computer> computerform = form(computer.class).fill( computer.find.byid(id) ); homecoming ok( editform.render(id, computerform) ); } /** * handle 'edit form' submission * * @param id id of computer edit */ public static result update(long id) { form<computer> computerform = form(computer.class).bindfromrequest(); if (computerform.haserrors()) { homecoming badrequest(editform.render(id, computerform)); } computerform.get().update(id); flash("success", "computer " + computerform.get().name + " has been updated"); homecoming go_home; } /** * display 'new computer form'. */ public static result create() { form<computer> computerform = form(computer.class); homecoming ok( createform.render(computerform) ); } /** * handle 'new computer form' submission */ public static result save() { form<computer> computerform = form(computer.class).bindfromrequest(); if (computerform.haserrors()) { homecoming badrequest(createform.render(computerform)); } computerform.get().save(); flash("success", "computer " + computerform.get().name + " has been created"); homecoming go_home; } /** * handle computer deletion */ public static result delete(long id) { computer.find.ref(id).delete(); flash("success", "computer has been deleted"); homecoming go_home; } }
the add-on add together @constraints.required company field of computer model, computer should not added without beingness attached company, field not beingness validated in form, computer still gets added without having company. bug or doing wrongly
java playframework-2.3
Comments
Post a Comment