【问题标题】:Native JavaScript validaton of form inputs in JSFJSF 中表单输入的原生 JavaScript 验证
【发布时间】:2012-02-27 10:03:04
【问题描述】:

我一直在学习 JSF,甚至不是弱者,所以我对此很陌生。我已经发现了一个“问题”……嗯,这实际上并不是什么大问题,而是一个外观问题。

我已经用一些验证标签制作了基本的表单来注册用户(参见上面的代码)。一切正常,但我也想通过 JavaScript 使用客户端控制。我已经发现我可以编写自己的 JavaScript 检查函数——但这不是我想要的。我已经在验证标签中定义了验证规则,这就是为什么我认为手动编写代码是愚蠢的,而像 JSF 这样好的框架能够自动完成大部分这些事情。我还发现 Sun 故意从 JSF 中删除了 JavaScript 自动验证。

所以我的问题是:有什么方法可以让 JSF 在表单提交时自动使用它们生成 JavaScript 函数(带有 f:validate 信息)?另外,我用的是PrimeFaces,有没有可能用PrimeFaces来实现呢?

这是我制作的表格代码:

<h:form>
    <table>
        <tr>
            <th><label for="registration-email">#{usersTexts.email}:</label></th>
            <td>
                <h:inputText value="#{userRegistry.newUser.email}" id="registration-email" validatorMessage="#{msg.invalidEmail}" maxlength="100">
                    <f:validateRegex pattern="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$" />
                </h:inputText>
            </td>
        </tr>
        <tr>
            <th><label for="registration-name">#{usersTexts.name}:</label></th>
            <td>
                <h:inputText value="#{userRegistry.newUser.name}" id="registration-name" validatorMessage="#{formsTexts.field} #{usersTexts.name} #{msg.isEmpty}" maxlength="100">
                    <f:validateRequired />
                </h:inputText>
            </td>
        </tr>
        <tr>
            <th><label for="registration-password1">#{usersTexts.password}:</label></th>
            <td>
                <h:inputSecret value="#{userRegistry.newUser.password}" binding="#{password}" id="registration-password1" validatorMessage="#{formsTexts.field} #{usersTexts.password} #{msg.isEmpty}">
                    <f:validateRequired />
                </h:inputSecret>
            </td>
        </tr>
        <tr>
            <th><label for="registration-password2">#{usersTexts.passwordAgain}:</label></th>
            <td>
                <h:inputSecret id="registration-password2" validatorMessage="#{msg.passwordsNotEqual}">
                    <f:validateLength minimum="#{user.MIN_PASSWORD_LENGTH}" />
                    <f:validator validatorId="equality" />
                    <f:attribute name="matchAgainst" value="#{password}" />
                </h:inputSecret>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <p:captcha immediate="true" validatorMessage="#{msg.wrongCaptcha}" requiredMessage="#{formsTexts.captcha} #{msg.isEmpty}" />
            </td>
        </tr>
        <tr>
            <td colspan="2" class="submit_row"><h:commandButton value="#{usersTexts.register}" action="#{userRegistry.register}" /></td>
        </tr>
    </table>
</h:form>

【问题讨论】:

    标签: java jsf primefaces


    【解决方案1】:

    JSF 规范没有定义或提供任何原生 JavaScript 验证。

    如果您想要的只是即时反馈,只需添加一个

    <f:ajax event="blur" render="messages" /> 
    

    在有问题的输入字段和一个

    <f:ajax execute="@form" render="@form" /> 
    

    在命令按钮内。

    【讨论】:

    • 这实际上不起作用...提交时我收到此错误:malformedXML:组件返回失败代码:0x80004005 (NS_ERROR_FAILURE),并且在模糊时没有任何反应。
    • 您是否在模板中使用&lt;h:head&gt; 而不是&lt;head&gt;&lt;h:head&gt; 允许 JSF(和组件库)自动包含必要的 CSS/JS 文件。
    • 是的,我愿意。实际上它只是在模板中,而不是在具体页面中,但这不是问题...尝试将 也添加到那里,问题仍然存在。
    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多