0

我有一个使用客户端验证的 XPage。如果验证失败,它会给用户一条alert消息,并且不允许处理服务器端的东西。我遇到的问题是无法用客户端“分配”服务器端变量。例如,考虑我有一个这样的 xp 输入字段:

<xp:inputText 
  styleClass="doc_field_textinput" id="input_part_title" type="text" size="40" 
  disableClientSideValidation="true" >
</xp:inputText>

我使用一个按钮来验证,如果验证成功 - 保存这个:

<xp:button id="save_part_btn" value="+Add this" style="float:right;">
             <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:

                    var estdoc:NotesDocument=database.getDocumentByUNID(doc_source.getDocument().getParentDocumentUNID())
                    var estPartdoc:NotesDocument=estdoc.getParentDatabase().createDocument()

                    estPartdoc.replaceItemValue('Form','Estimate_Cost_Part')
                    estPartdoc.replaceItemValue('Predoc',estdoc.getUniversalID())
                    estPartdoc.replaceItemValue('$OSN_IsSaved','1')

                    estPartdoc.replaceItemValue('Title', getComponent('input_part_title').getValue())

                    }]]>
            </xp:this.action>
                <xp:this.script><![CDATA[ 
                var result = "";
                var wholeResult = true;

                function isStringEmpty(string2Check) 
                {
                    return string2Check == "" || string2Check[0] == " ";
                }

                if(isStringEmpty(document.getElementById("#{id:input_part_title}").value)) 
                {
                    wholeResult = false;
                    result += 'The field cannot be empty!'
                }

                result = result.replace(/\n$/, "")

                if(!wholeResult) 
                {
                    alert(result)
                }

                return wholeResult;

                ]]>
                </xp:this.script>
             </xp:eventHandler>
</xp:button>

不幸的是,input_part_title在任何情况下,服务器端的总是null,而document.getElementById("#{id:input_part_title}").value工作得很好,并且确实按照预期的方式工作。我希望我可以将同一行代码添加到我的服务器端,但我不能,因为document它是服务器端的未知属性。有什么方法可以以某种方式input_part_title从客户端分配一个值吗?

4

1 回答 1

0

最佳实践是使用value组件的属性将其绑定到某个服务器端元素,例如 dominoDocument 数据源上的字段或viewScope变量。然后就可以参考了。

另一种方法是使用getComponent("input_part_title").getValue().

将验证器附加到组件将确保在服务器上的 Process Validations 阶段进行验证。如果您有非文本输入组件(例如取一个数字),该阶段也会运行转换器检查并相应地中止。这将中止处理,因此按钮的 eventHandleraction属性中的代码根本不会运行。它还将处理将错误消息返回给浏览器(以编程方式,您需要查看facesContext.addMessage()并突出显示无效的组件(getComponent.setValid(false))。验证器会为您处理所有这些。

于 2018-01-22T13:23:23.763 回答