Play framework form validation errors
February 23, 2014 Leave a comment
I post as I work my way through the Play material. I have not still read The reactive manifesto and relate it to Play. So I used the same classic trick popularized by Struts to debug this. As far as this type of error display mechanism is concerned I did not find anything different between Play and old versions of Struts.
Problem
I did not initialize isApproved to false.
package models; import javax.persistence.*; import javax.validation.Valid; import play.data.validation.Constraints.MaxLength; import play.data.validation.Constraints.MinLength; import play.data.validation.Constraints.Required; import play.db.ebean.*; @Entity public class Proposal extends Model { @Id public String id; @Required public String title; @MinLength(value = 10) @MaxLength(value = 1000) @Column(length=1000) public String proposal; @Required public SessionType type = SessionType.OneHourTalk; @Required public Boolean isApproved; public String keywords; @Valid @OneToOne(cascade=CascadeType.ALL) public Speaker speaker; }
Display errors
@(proposal: play.data.Form[Proposal]) @import helper._ @main("New Proposal"){ @if(proposal.hasErrors) { <div class="alert alert-error"> @proposal.errors </div> } @form(action = routes.MainController.submitProposal()){ <h3>Proposal</h3> @inputText(proposal("title")) @textarea(proposal("proposal")) @inputText(proposal("keywords")) <h3>Speaker</h3> @inputText(proposal("speaker.name")) @inputText(proposal("speaker.email")) @textarea(proposal("speaker.bio")) @inputText(proposal("speaker.twitterId")) @inputText(proposal("speaker.pictureUrl")) <p class="submit"> <button type="submit" class="button green" id="submitForm">Submit</button> </p> } }
Error message
I can customize it but this sufficed.