[英]Validate h:selectOneMenu and t:inputFileUpload with f:ajax blur
I want to validate, before submitting the form, if the h:selectOneMenu and t:inputFileUpload have a valid value. 我想在提交表单之前验证h:selectOneMenu和t:inputFileUpload是否具有有效值。 I have this code but it does not show the h:message when I leave the selectOneMenu, the same thing happens with t:fileInputUpload.
我有这段代码,但是当我离开selectOneMenu时,它没有显示h:message ,t:fileInputUpload也发生了同样的事情。
<h:form id="uploadForm" enctype="multipart/form-data">
<h:panelGrid columns="3">
<h:outputLabel for="option" value="Operación:" />
<h:selectOneMenu id="option" value="#{menu.infoMenuItem}"
required="true"
converterMessage="Opción no valida !" >
<f:selectItem itemLabel="Seleccione una opcion..." itemValue="null"/>
<f:selectItems value="#{menu.infoMenuItems}" />
<f:ajax event="blur" render="optionMessage" />
</h:selectOneMenu>
<h:message id="optionMessage" for="option" style="color: #FF0000;" />
<h:outputLabel for="upfile" value="Archivo: " />
<t:inputFileUpload id="upfile" value="#{uploadFile.upFile}"
required="true">
<f:ajax event="blur" render="uploadMessage" />
</t:inputFileUpload>
<h:message id="uploadMessage" for="upfile" style="color: #FF0000" />
<h:panelGroup />
<h:commandButton value="Continuar"
action="#{uploadFile.upload}">
</h:commandButton>
</h:panelGrid>
</h:form>
and also, is there a way to prevent the form to be submitted if the previous components don't have valid values? 并且,如果以前的组件没有有效值,是否有一种方法可以阻止提交表单?
Cheers ! 干杯!
I'm using Mojarra 2.1.4 and Tomahawk 1.1.11 (for upload) 我正在使用Mojarra 2.1.4和Tomahawk 1.1.11(用于上传)
it does not show the h:message when I leave the selectOneMenu
当我离开selectOneMenu时,它不显示h:message
The code which is posted so far looks fine. 到目前为止发布的代码看起来不错。 The problem is caused elsewhere.
该问题是在其他地方引起的。 Perhaps you're nesting HTML
<form>
s, which is invalid. 也许您正在嵌套HTML
<form>
,这是无效的。 Make sure that you don't nest JSF <h:form>
components. 确保您不嵌套JSF
<h:form>
组件。 For other causes, see also commandButton/commandLink/ajax action/listener method not invoked or input value not updated . 对于其他原因,另请参见未调用commandButton / commandLink / ajax操作/侦听器方法或未更新输入值 。
the same thing happens with
t:fileInputUpload
.t:fileInputUpload
发生相同的情况。
Unfortunately, the HTML4 <input type="file">
element and XHR1 ajax don't play very well together. 不幸的是,HTML4
<input type="file">
元素和XHR1 ajax不能很好地配合使用。 Determining whether a file is selected requires a multipart/form-data
ajax request, which is only supported in HTML5/XHR2 (as far now, PrimeFaces 3.0 is the only JSF component library which supports this -in theory! I haven't tested it). 确定是否选择文件需要
multipart/form-data
ajax请求,只有HTML5 / XHR2支持该请求(到目前为止,PrimeFaces 3.0是唯一支持此功能的JSF组件库-从理论上讲,我还没有对其进行测试! )。 So the ajax validation request is totally ignored by <t:inputFileUpload>
. 因此,
<t:inputFileUpload>
完全忽略了ajax验证请求。 Since JSF/Tomahawk is just a HTML code generator, it can't do any much for you here. 由于JSF / Tomahawk只是HTML代码生成器,因此它在这里不能为您做任何事情。 Best what you can do is to validate it by plain vanilla JS/HTML.
最好的办法是通过纯净的原始JS / HTML对其进行验证。
<t:inputFileUpload id="upfile" value="#{bean.file}" required="true"
onblur="document.getElementById('uploadMessage').style.display = (!value) ? 'block' : 'none'"
/>
<h:panelGroup>
<h:message for="uploadMessage" style="color: #FF0000" />
<span id="uploadMessage" style="display: none; color: #FF0000">Seleccione una archivo...</span>
</h:panelGroup>
(the <h:message>
is kept in place so that the server side validation message will be shown anyway whenever you press the command button -which fires a synchronous (non-ajax) request) (
<h:message>
保留在适当位置,以便无论何时按下命令按钮都将显示服务器端验证消息,这会触发同步(非ajax)请求)
Unrelated to the concrete problem, you should prefer putting JS and CSS code in its own .js
and .css
files which you include by <script>
and <link>
in <h:head>
. 与具体问题无关 ,您应该更喜欢将JS和CSS代码放在自己的
.js
和.css
文件中,该文件由<h:head>
的<script>
和<link>
包括。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.