Struts validation variable back referencing

Old Struts 1.x project.  I’m new to Struts and I was trying to validate a string with minlength, and if the length is not enough, then display an error message which contains the original string and the length of the original string. Not because it makes sense, but because that’s what I am asked to do. And I’m trying to do this with the validation XML, because all of the project’s validation stuff is declared in XML files.
Struts has a relatively good documentation, with examples and everything, but they don’t really mention anywhere how their extra stuff, like *this* works. Anyway, *this* was the closest thing that I could find to solve my problem, so I gave it a try. After a few tries my validation XML looked like this:

<field property="input" depends="minlength">
    <arg0 key="search.string" />
    <arg1 name="minlength" key="${var:minlength}" resource="false" />
    <arg2 name="minlength" key="${var:input}" resource="false" />
    <arg3 name="minlength" key="${var:length}" resource="false" />

but it doesn’t work, it just put “*this*” and “(*this*).length()” into the error message. (Which is kind of understandable.)
Then I put the question on Stackoverflow, then nobody answered for a long time. And after consulting with a senior developer I came to the conclusion that this most likely can’t be done with a validation XML file. I put this here so if others try to still do something like this in legacy code like this. But there are other ways to validate in Struts too and they are more flexible than the XML.
Just in case anybody has to do anything like this on a legacy project… there you go.

Leave a Reply

Your email address will not be published. Required fields are marked *