使用客户端访问和设置服务器端变量的方法

问题描述 投票:0回答:1

我有一个使用客户端验证的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总是为空,而document.getElementById("#{id:input_part_title}").value工作得很好,并且真正按照预期的方式工作。我希望我可以向服务器端添加相同的代码行,但我不能,因为document是服务器端的未知属性。有什么方法我可以以某种方式将input_part_title分配给客户端的值?

javascript validation xpages ssjs
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.