Combining Validate Method and xml validation in St

2019-08-23 03:50发布

问题:

Is it possible to combine both xml validation and Validate method for form validation?

my general thought is that the xml validation will just mostly contain how long should a given field name is, is it a digit, is it a valid email address?. While the Validate method will check a given fieldname value if that value exist in the database it would then an error.

if so please show an example.

回答1:

AFAIK, no. According to comments, yes.

I've always thought that Validation is intended to validate the correctness of the input, not the "semantic": If input is wrong, go back; if input is fine, check on db for duplicates, check user roles for permissions, call services, look into file system, etc.

By the way, note this: with fieldexpression validator, you can call functions with parameters, like in JSP (it is OGNL).

It would be probably a bad practice (read above), but you could do something like this:

<validators>
    <field name="username">
        <field-validator type="fieldexpression">
            <param name="expression">
                <![CDATA[
                    isUserAlreadyInDatabase(username)
                ]]>
            </param>
            <message>function call message here</message>
        </field-validator>
    </field>
</validators>

I've shared this because it is worth knowing and it can be really useful in some cases (definitely not the one pointed out by your question).

For example i used this to check if Dates from page were in the interval betweeen startDate and endDate of the current Subject. startDate and endDate was instance variables from the Action, read through getters...

Read this link too: Can we call methods with parameters in Struts2 fieldexpression?

Hope that helps