Jaryn C.
Jaryn C.

Reputation: 65

Play Framework 1.2.4 : Checking for the existence of specific errors

I'm trying to display a generic error message when validation fails on certain fields, and more specific messages when other fields fail. However, I haven't been able to figure out a way to check for the existence of multiple errors by their keys.

I know that I can check for a single error with #{ifError 'key'} and I can check for multiple errors with #{ifErrors}, but how do I check for multiple error keys, something like #{if (error 'keyA' || error 'keyB')}? Multiple if statements won't work, as I would like the generic message displayed only once if certain errors are present.

What I'm trying to do is shown in the below snippet:

<!-- Only if there's an error on the 'specific' field will this div 
     be populated. -->
<div class="specificError">#{error 'specific' /}</div>

<!-- If there are other errors, display a generic error message.
     This if statement won't compile but shows what I want to do. -->
#{if (error 'fieldA' || error 'fieldB')}
    <div class="genericValidateError">&{'error.validation'}</div>
#{/if}

All suggestions are welcome, including those proposing alternative validation methods.

Upvotes: 1

Views: 715

Answers (2)

marcospereira
marcospereira

Reputation: 12214

Maybe the best way to do it is creating your own FastTag. At least I think it is a better alternative (easy to test and reuse in different views). You can see how to create your own fast tags here: custom java tags.

Upvotes: 0

Codemwnci
Codemwnci

Reputation: 54884

You can access the validation object to check the fields directly as follow

#{if play.data.validation.Validation.current().hasError("fieldA") || 
     play.data.validation.Validation.current().hasError("fieldB") }

   <div class="genericValidateError">&{'error.validation'}</div>
#{/if}

Upvotes: 3

Related Questions