Bean验证ScriptAssert不起作用

时间:2017-09-28 02:25:04

标签: bean-validation

我不确定ScriptAssert是否可以应用于Field。我正在尝试下面的ScriptAssert但是它一直返回消息“Value is Mandatory”甚至_this.OBJSTATE!='CANCELED'。有人可以帮忙吗?

<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                     xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    <default-package>x.x</default-package>

    <bean class="x.class" ignore-annotations="true">
        <field name="OBJSTATE">
            <constraint annotation="javax.validation.constraints.Pattern">
                <message>Value should be CANCELLED/RELEASED</message>
                <element name="regexp">^(CANCELLED)$|^(RELEASED)$</element>
            </constraint>
            <constraint annotation="javax.validation.constraints.NotNull">
                <message>Field is Mandatory</message>
            </constraint>
            <constraint annotation="org.hibernate.validator.constraints.NotBlank">
                <message>Value is Mandatory</message>
            </constraint>
        </field>
        <field name="T1PONumber">
            <constraint annotation="org.hibernate.validator.constraints.ScriptAssert" >
                <message>Value is Mandatory</message>
                <element name="lang">javascript</element>
                 <element name="script" >
                     <value>_this.OBJSTATE == 'CANCELLED'</value>
                 </element>
            </constraint>

        </field>

    </bean>
</constraint-mappings>

1 个答案:

答案 0 :(得分:0)

ScriptAssert应该应用于类级别

<bean class="x.class" ignore-annotations="true">
    <class ignore-annotations="false" >
        <constraint annotation="org.hibernate.validator.constraints.ScriptAssert">
            <message>T1PONumber value is Mandatory</message>
        <element name="lang">javascript</element>
        <element name="script" >
            <value>_this.OBJSTATE != 'CANCELLED'</value>
        </element>
        </constraint>
    </class>
    <field name="OBJSTATE">
        <constraint annotation="javax.validation.constraints.Pattern">
            <message>Value should be CANCELLED/RELEASED</message>
            <element name="regexp">^(CANCELLED)$|^(RELEASED)$</element>
        </constraint>
        <constraint annotation="javax.validation.constraints.NotNull">
            <message>Field is Mandatory</message>
        </constraint>
        <constraint annotation="org.hibernate.validator.constraints.NotBlank">
            <message>Value is Mandatory</message>
        </constraint>
    </field>
    <field name="T1PONumber">


    </field>

</bean>