Fatih Donmez
Fatih Donmez

Reputation: 4347

multiple verify method on form tuple

I'm quite new to play and scala. I'm working on form and validations. But I couldn't figure out to get all errors from multiple verification on form.

My form tuple looks like;

val companyMapping = Forms.tuple(
    "name" -> nonEmptyText,
    "email" -> email,
    "password" -> nonEmptyText(8),
    "re-password" ->nonEmptyText(8)).verifying(
        // Add an additional constraint: both passwords must match
        "Passwords don't match", data => {
        data._3 == data._4  }
    ).verifying(
        // Second constraint
        "Test error", data => {
            false  }
    )

In the view I print global errors and errors, it looks like;

@println(companyForm.globalError)
@println(companyForm.errors)

and output;

Some(FormError(,Passwords don't match,WrappedArray()))
List(FormError(,Passwords don't match,WrappedArray()), FormError(,Test error,WrappedArray()))

At this stage I have absolutely no idea about how to print both of the errors. I'm showing errors separately for the each input and show global errors at the end. But if passwords match I can see test constraint in the global errors. Other than it only shows password match constraint.

Here is the view part;

@helper.form(action = routes.Login.register) {

        <div class="row">
            <span class="label">Name</span>
            <input type="text" name="name" placeholder="Company Name" value="@companyForm("name").value" >
            @if(!companyForm.errors("name").isEmpty){

                <span class="error">@Messages(companyForm.errors("name")(0).message,"Company name")</span>
            }
        </div>

        <div class="row">
            <span class="label">Email</span>
            <input type="text" name="email" placeholder="Email" value="@companyForm("email").value" >
            @if(!companyForm.errors("email").isEmpty){
                <span class="error">@Messages(companyForm.errors("email")(0).message,companyForm.errors("email")(0).key)</span>
            }

        </div>

        <div class="row">
            <span class="label">Password</span>
            <input type="password" name="password" placeholder="Password" value="@companyForm("password").value" >
            @if(!companyForm.errors("password").isEmpty){
                <span class="error">@Messages(companyForm.errors("password")(0).message,8)</span>
            }
        </div>

        <div class="row">
            <span class="label">Re-type Password</span>
            <input type="password" name="re-password" placeholder="Re-type your password" value="@companyForm("re-password").value" >
            @if(!companyForm.errors("re-password").isEmpty){
            <span class="error">@Messages(companyForm.errors("re-password")(0).message,8)</span>
            }
        </div>
                   @println(companyForm.globalError)
                   @println(companyForm.errors)
        <div class="row">
            <span class="label"><button type="submit">Save</button></span>
            @companyForm.globalError.map { error =>
               <span class="error">@error.message</span>
            }
        </div>

     }

Maybe I'm just confused about those error types. So please can you explain it detailed.

Upvotes: 0

Views: 778

Answers (1)

gourlaysama
gourlaysama

Reputation: 11290

In the re-password section of your template, you currently test if !companyForm.errors("re-password").isEmpty but then only show the message for companyForm.errors("re-password")(0), i.e. the first error only. Even if you have multiple errors.

You have to iterate over companyForm.errors("re-password") to print something for each error.

You can for example output a <span class="error">... for each error, using a for comprehension:

<div class="row">
    <span class="label">Re-type Password</span>
    <input type="password" name="re-password" placeholder="Re-type your password" value="@companyForm("re-password").value" >
    @for (error <- companyForm.errors("re-password")) {
    <span class="error">@Messages(error.message,8)</span>
    }
</div>

See the play doc for Scala templates for other useful syntax to use in templates.

Upvotes: 1

Related Questions