在字段旁边显示 JSF 验证错误

发布于 2024-12-28 11:19:53 字数 2042 浏览 2 评论 0原文

我想在文本字段旁边显示错误消息。

我能够显示错误消息,但所有错误消息同时出现...

<h:form id="LoginForm">
        <table>
            <tr>
                <td colspan="4" id="login">
                    <h2 style="border-bottom: 1px solid #CCCCCC;">Login</h2>
                    <tr>
                        <td class="label"> 
                            <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
                        </td>
                        <td class="field">
                            <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">
                            <h:outputLabel for="password" value="Password:"></h:outputLabel>
                        </td>
                        <td class="field">
                            <h:inputSecret id="password"  size="20" maxlength="50" required="true" requiredMessage="Please confirm your password" value="#{loginController.selected1.password}"><f:validateLength minimum="6" maximum="50"></h:inputSecret>
                        </td>
                    </tr>
                </td>
            </tr>
                <tr>
                    <td>
                        <h:commandButton value="Login" type="submit" action="#{loginController.checkValidUser()}"></h:commandButton>
                    </td>
                </tr>
        </table>

    </h:form>

如何修改代码以在文本字段旁边显示错误消息?

I want to display the error messages beside the text fields.

I am able to show the error message but all the error messages come at once...

<h:form id="LoginForm">
        <table>
            <tr>
                <td colspan="4" id="login">
                    <h2 style="border-bottom: 1px solid #CCCCCC;">Login</h2>
                    <tr>
                        <td class="label"> 
                            <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
                        </td>
                        <td class="field">
                            <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">
                            <h:outputLabel for="password" value="Password:"></h:outputLabel>
                        </td>
                        <td class="field">
                            <h:inputSecret id="password"  size="20" maxlength="50" required="true" requiredMessage="Please confirm your password" value="#{loginController.selected1.password}"><f:validateLength minimum="6" maximum="50"></h:inputSecret>
                        </td>
                    </tr>
                </td>
            </tr>
                <tr>
                    <td>
                        <h:commandButton value="Login" type="submit" action="#{loginController.checkValidUser()}"></h:commandButton>
                    </td>
                </tr>
        </table>

    </h:form>

How to modify the code to show error messages beside the text fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2025-01-04 11:19:53

只需将 放在您需要查看的位置即可。例如

<tr>
    <td class="label"> 
        <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
    </td>
    <td class="field">
        <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
        <h:message for="email" />
    </td>
</tr>

,或者

<tr>
    <td class="label"> 
        <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
    </td>
    <td class="field">
        <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
    </td>
    <td class="message">
        <h:message for="email" />
    </td>
</tr>

与具体问题无关,我建议查看。它最大限度地减少了丑陋的 HTML 表格样板。您的代码最终将如下所示:

<h:form id="LoginForm">
    <h2>Login</h2>
    <h:panelGrid columns="3" columnClasses="label,field,message">
        <h:outputLabel for="email" value="Login Email:" />
        <h:inputText id="email" value="#{loginController.selected1.email}" size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]">
            <f:validateLength maximum="70" />
        </h:inputText>
        <h:message for="email" />

        <h:outputLabel for="password" value="Password:" />
        <h:inputSecret id="password" value="#{loginController.selected1.password}" size="20" maxlength="50" required="true" requiredMessage="Please confirm your password">
             <f:validateLength minimum="6" maximum="50" />
        </h:inputSecret>
        <h:message for="password" />

        <h:panelGroup />
        <h:commandButton value="Login" action="#{loginController.checkValidUser()}" />
        <h:panelGroup />
    </h:panelGrid>
</h:form>

Just put a <h:message> right there where you need to see it. E.g.

<tr>
    <td class="label"> 
        <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
    </td>
    <td class="field">
        <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
        <h:message for="email" />
    </td>
</tr>

or

<tr>
    <td class="label"> 
        <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
    </td>
    <td class="field">
        <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{loginController.selected1.email}"> <f:validateLength maximum="70"></f:validateLength></h:inputText>
    </td>
    <td class="message">
        <h:message for="email" />
    </td>
</tr>

Unrelated to the concrete problem, I suggest to have a look at <h:panelGrid>. It minimizes the ugly HTML table boilerplate. Your code would then end up like follows:

<h:form id="LoginForm">
    <h2>Login</h2>
    <h:panelGrid columns="3" columnClasses="label,field,message">
        <h:outputLabel for="email" value="Login Email:" />
        <h:inputText id="email" value="#{loginController.selected1.email}" size="20" maxlength="70" required="true" requiredMessage="Please enter your email,eg:[email protected]">
            <f:validateLength maximum="70" />
        </h:inputText>
        <h:message for="email" />

        <h:outputLabel for="password" value="Password:" />
        <h:inputSecret id="password" value="#{loginController.selected1.password}" size="20" maxlength="50" required="true" requiredMessage="Please confirm your password">
             <f:validateLength minimum="6" maximum="50" />
        </h:inputSecret>
        <h:message for="password" />

        <h:panelGroup />
        <h:commandButton value="Login" action="#{loginController.checkValidUser()}" />
        <h:panelGroup />
    </h:panelGrid>
</h:form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文