表单验证的即时属性在JSF中产生意外结果

时间:2018-10-27 05:54:28

标签: jsf

我检查了this postthis post,但是这些解决方案都无法为我在本帖子中遇到的问题提供明确的答案。请尝试以下示例。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form id="employeeForm">
            <h:panelGrid columns="3">
                <h:outputLabel for="employeeFirst" value="First: " />
                <h:inputText id="employeeFirst" immediate="true" onchange="submit()">
                    <f:validateLength minimum="3" maximum="30" />
                </h:inputText>
                <h:message for="employeeFirst" errorStyle="color: red" />
                <h:outputLabel for="employeeLast" value="Last: " />
                <h:inputText id="employeeLast">
                    <f:validateLength minimum="3" maximum="30" />
                </h:inputText>
                <h:message for="employeeLast" errorStyle="color: red" />
                <h:outputLabel for="employeeTitle" value="Title" />
                <h:inputText id="employeeTitle">
                    <f:validateLength minimum="3" maximum="30" />
                </h:inputText>
                <h:message for="employeeTitle" errorStyle="color: red" />
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

如果您运行上面的代码,并在第一个输入字段中插入了少于3个字符的值,则该字段旁边将显示红色错误消息。这种行为绝对是可以预期的。

但是,如果您插入的长度介于3到30个字符之间,则第一个字段的错误消息就会消失,而其他两个输入字段会自动生成错误消息。我不希望在将值成功插入第一个输入字段后自动显示这两个错误消息。

我认为第二个和第三个输入字段的即时属性默认情况下设置为true,因此我将即时属性放入其中并为其分配了false,但结果与以前相同。

在表单中如何只让一个字段立即采取行动,而没有其他字段呢?


编辑:

很抱歉,我的问题不清楚。我已经知道ajax可以处理用于表单验证的错误消息,但是我想让JSF规范负责人知道immediate属性可能存在问题,以便他们可以在下一个JSF版本中对其进行修复。我还希望使用immediate属性可以解决这种意外行为,在这种情况下,我想通过这篇文章来听听。无论如何,谢谢您建议我为此使用Ajax。

1 个答案:

答案 0 :(得分:0)

请使用以下代码。您需要删除onchange =“ submit()。 无需提交所有内容,而是可以指定需要使用f:ajax进行处理和更新的确切组件。您需要指定需要按属性执行来处理的组件ID列表,以及需要按属性渲染来更新的组件列表。

<h:outputLabel for="employeeFirst" value="First: " />
<h:inputText id="employeeFirst">
    <f:validateLength minimum="3" maximum="30" />
    <f:ajax execute="@this" render="@this errorMessageBlock" event="blur" />
</h:inputText>
<h:message for="employeeFirst" id="errorMessageBlock" errorStyle="color: red" />