简体   繁体   English

表单验证清除JavaScript字段

[英]Form validation clears JavaScript field

I am using a SEAM based JSF web application. 我正在使用基于SEAM的JSF Web应用程序。 I have a simple form that includes a lookup (pop up window) that retrieves a Category name and ID (both are retrieved with no issues and are stored in myView). 我有一个简单的表单,其中包含一个查找(弹出窗口),用于检索类别名称和ID(两者都检索没有问题,并存储在myView中)。 When the look up returns, the Category name is used to populate "selectedCategoryName" (using Jquery). 当查找返回时,类别名称用于填充“selectedCategoryName”(使用Jquery)。

The issue comes when you try to submit the form, and another field fails validation (simple required field missing). 当您尝试提交表单时出现问题,另一个字段未通过验证(缺少简单的必填字段)。 All of my other fields remain unchanged, but selectedCategoryName is cleared (even though the bean still has it's value, so fixing the validation error and resubmitting resolves to a proper submit). 我的所有其他字段保持不变,但selectedCategoryName被清除(即使bean仍然具有它的值,因此修复验证错误并重新提交解析为正确的提交)。 So my question is, how can I maintain showing the value that's in "selectedCategoryName" when the form validation fails? 所以我的问题是,当表单验证失败时,如何保持显示“selectedCategoryName”中的值?

Thanks in advance! 提前致谢!

<s:decorate id="selectedCategoryField" template="/layout/edit.xhtml">
<span>
    <h:panelGroup rendered="#{! myView.persisted}">
    <img class="lookup" src="${request.contextPath}/images/lookup.gif" width="15" height="14" alt="" 
                        title="Category Lookup" 
                        onclick="return openNewWindow('${CategoryLookupUrl}?#{myView.lookupParameters}', 'id');"/>
    </h:panelGroup>

            <h:inputText id="selectedCategoryName" required="true" 
                        value="#{myView.selectedCategoryName}" 
                        size="50" 
                        readonly="true">

            <a:support event="onchanged" reRender="selectedCategoryField" ajaxSingle="true"/>
    <a:support event="oninputchange" reRender="selectedCategoryField" ajaxSingle="true"/>
            </h:inputText>
    </span>

The problem is, that actually the bean does not hold the selected value because of readonly="true" . 问题是,由于readonly="true" ,实际上bean没有保持选定的值。 When you apply that property to an UIInput the UIInput will not be processed on a submit. 将该属性应用于UIInput时,将不会在提交上处理UIInput。

To fix this you can do the following: 要解决此问题,您可以执行以下操作:
If you use JSF2 you can use readonly="#{facesContext.renderResponse}" instead. 如果您使用JSF2,则可以使用readonly="#{facesContext.renderResponse}"代替。
If not, define a method isReadonly on your backing bean and use readonly="#{myView.isReadonly}" . 如果没有, isReadonly在您的支持bean上定义一个方法isReadonly并使用readonly="#{myView.isReadonly}"

public boolean isReadonly() {
    return FacesContext.getCurrentInstance().getRenderResponse();
}

Have a look at the following similiar question and especially the answer for more details on why this works: Data in <h:inputText readonly="true"> disappears when command button is clicked 看一下下面这个类似的问题,尤其是答案,了解更多有关其工作原理的详细信息: 单击命令按钮时<h:inputText readonly =“true”>中的数据消失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM