XPages custom validator

2020-03-31 09:12发布

Ultimately I need to do some cross field validation and thought I would use a custom validator to do this. But I can't even get a simple example to work. The following code (which is pretty similar to that on p.116 of "Mastering XPages" allows any value (including an empty field). Am I missing something obvious?

            <xp:inputText
                id="field1"
                value="#{document1.field1}">
                <xp:this.validators>
                    <xp:customValidator>
                        <xp:this.validate><![CDATA[#{javascript:if (value == "") {
return new javax.faces.application.FacesMessage("Please enter a value");
}}]]></xp:this.validate>
                    </xp:customValidator>
                </xp:this.validators>
            </xp:inputText>

标签: xpages
2条回答
SAY GOODBYE
2楼-- · 2020-03-31 09:24

It is not possible to validate an empty field with a validator. A validator runs only if a value exist. In XPages you have the required property for fields which allows to check for empty fields; this is a workaround for this problem, and it is (as far as I know) not possible to create your own "required" validator.

If you want to create your own one, you have to create a converter instead

UPDATE 21.06.2013

It is possible to create an own required validator with a small workaround: http://hasselba.ch/blog/?p=764

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-03-31 09:43

You need to return a string with the error message in it - and not a FacesMessage object.

So in your case, do this instead:

<xp:inputText id="field1" value="#{document1.field1}">
    <xp:this.validators>
        <xp:customValidator>
            <xp:this.validate><![CDATA[#{javascript:
                if (value == "") {
                    return "Please enter a value";
                }
            }]]></xp:this.validate>
        </xp:customValidator>
    </xp:this.validators>
</xp:inputText>
查看更多
登录 后发表回答