Expression error

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Expression error

Carlo
Hi,

I am currently binding a list object in a form. The object I am binding to has a list called answers and my validator seems to be working where it is setting field errors of answers[0].value for example if the first element were to have an error. am having issues with the #fields object and just wondering whether there was a way around this. So I am using the th:classappend tag with the following expression:

<div class="form-group required" th:classappend="${#fields.hasErrors('answers[' + ${questionStat.index} + '].value'})}? 'has-error'" th:each="question : ${questions}">

My desired output is to have answers[0].value but I want to substitute the 0 value for the index value of the loop I am going through.

I am using SpringMVC for this and do have validators in other parts of my project but none which are used with validating list elements.

Any help would be much appreciated
Reply | Threaded
Open this post in threaded view
|

Re: Expression error

Metroids
Try

th:each="question,questionStat: ${questions}"
and
th:classappend="${#fields.hasErrors('answers[' + questionStat.index + '].value') ? 'has-error' : ''}"

Mostly just formatting issues, and I think you forgot to put questionsStat as the stats variable in the th:each.
Reply | Threaded
Open this post in threaded view
|

Re: Expression error

Zemi
Administrator
In reply to this post by Carlo
Hello,

you could try to use preprocessing, something like

    ${#fields.hasErrors('__${answers[questionStat.index].value}__')}

Maybe the final expression may be a bit different, but try to play with preprocessing, i.e., expressions like __${...}__

Regards,
  Zemi