Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent model validation on User Name in Scala Play! 2 form mapping in create and update
    primarykey
    data
    text
    <p>I have user mapping as follows (there are few others too)</p> <pre><code>val userMapping: Mapping[User] = mapping( "id" -&gt; ignored(NotAssigned: Pk[Long]), "title" -&gt; nonEmptyText, "name" -&gt; nonEmptyText, "userName" -&gt; nonEmptyText, "skype" -&gt; nonEmptyText, "emailId" -&gt; ignored("": String), "secondaryEmailId" -&gt; ignored("": String), "password" -&gt; ignored("": String), "position" -&gt; optional(text), "active" -&gt; boolean, "subscribeEmail" -&gt; boolean, "creationDate" -&gt; optional(date("yyyy-MM-dd")), "lastUpdatedDate" -&gt; optional(date("yyyy-MM-dd")) )(User.apply)(User.unapply) </code></pre> <p>The problem is if I apply validation on userName as</p> <pre><code>"userName" -&gt; nonEmptyText.verifying("User name is already taken", user =&gt; !User.findUserByUserName(user.userName).isDefined) </code></pre> <p>this works perfectly fine on user creation but on edit form this validation breaks. I want to re use the same mapping for both create and update.</p> <p>Currently I have moved it from form field to form level but thing is until all the form level error goes this validation is never reached and shown.</p> <p>My complete form mapping is as follows (Same problem with company name).</p> <pre><code>val registerForm:Form[(User,Company)] = Form( mapping( "user" -&gt; userMapping.verifying("User name is already taken", user =&gt; !User.findUserByUserName(user.userName).isDefined), "password" -&gt; passwordMapping, "company" -&gt; companyMapping.verifying("Company name is already registered", company =&gt; !Company.findCompanyByCompanyName(company.name).isDefined), "emailPair" -&gt; emailPairMapping ){(user,passwords,company,emailPair) =&gt; (user.copy(password = passwords._1,emailId = emailPair._1,secondaryEmailId = emailPair._2,active = true),company)} //binding apply {userCompanyTuple =&gt; Some(userCompanyTuple._1, ("",""),userCompanyTuple._2,(userCompanyTuple._1.emailId,userCompanyTuple._1.secondaryEmailId))} //un binding un apply ) </code></pre> <p>For edit case I am having mapping as (validation still to be applied)</p> <pre><code>val registerFormEdit:Form[(User,Company)] = Form( mapping( "user" -&gt; userMapping, "company" -&gt; companyMapping, "emailPair" -&gt; emailPairMapping ){(user,company,emailPair) =&gt; (user.copy(emailId = emailPair._1,secondaryEmailId = emailPair._2,active = true),company)} //binding apply {userCompanyTuple =&gt; Some(userCompanyTuple._1,userCompanyTuple._2,(userCompanyTuple._1.emailId,userCompanyTuple._1.secondaryEmailId))} //un binding un apply ) </code></pre> <blockquote> <p>Another challenge I see is how to get hold of id in edit validation as "id" is ignored. Will I have to handle the edit case in update action method?</p> <p>In case I'll have to do it in update action method sample snippet would be great as I am also confused how to add error messages in action method.</p> </blockquote> <p>Would be really great if someone provides input how this can be accomplished.</p> <p>I using Scala with Play! 2.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload