如果存在多个表单,如何将“addGlobalError()”分配给正确的表单(How to assign “addGlobalError()” to correct form if several f

编程入门 行业动态 更新时间:2024-10-16 18:36:57
如果存在多个表单,如何将“addGlobalError()”分配给正确的表单(How to assign “addGlobalError()” to correct form if several forms exists)

我在同一页面上有登录和注册表单,我尝试仅在相关表单上返回全局错误消息。 我该怎么做呢?

import org.omnifaces.util.Messages;

// login class submit()
Messages.addGlobalError('Login failed');

// register class submit()
Messages.addGlobalError('Register failed');
 
<h:form id="login-form">    
    <h:messages globalOnly="true" />    
    // [...]                            
    <h:commandButton value="Login" action="#{login.submit()}" />    
</h:form>    
<h:form id="register-form">    
    <h:messages globalOnly="true" />    
    // [...]                            
    <h:commandButton value="Sign up" action="#{register.submit()}" />    
</h:form>

I have a login and a register form on one and the same page and I try to return the global error message only on the concerning form. How to I do that correctly?

import org.omnifaces.util.Messages;

// login class submit()
Messages.addGlobalError('Login failed');

// register class submit()
Messages.addGlobalError('Register failed');
 
<h:form id="login-form">    
    <h:messages globalOnly="true" />    
    // [...]                            
    <h:commandButton value="Login" action="#{login.submit()}" />    
</h:form>    
<h:form id="register-form">    
    <h:messages globalOnly="true" />    
    // [...]                            
    <h:commandButton value="Sign up" action="#{register.submit()}" />    
</h:form>

                

最满意答案

这是一个矛盾的......你根本不会将全局错误分配给特定组件(表单一个组件)。

只需使用您分配给表单id的错误

Messages.addError(id, error);

添加<h:message for="...">其中for包含表单的id


以下是您的示例应如何显示:

import org.omnifaces.util.Messages;

// login class submit()
Messages.addError('login-form', 'Login failed');

// register class submit()
Messages.addError('register-form', 'Register failed');
 
<h:form id="login-form">    
    <h:messages globalOnly="true" for="login-from" />    
    // [...]                            
    <h:commandButton value="Login" action="#{login.submit()}"/>    
</h:form>    
<h:form id="register-form">    
    <h:messages globalOnly="true" for="register-from" />    
    // [...]                            
    <h:commandButton value="Sign up" action="#{register.submit()}"/>    
</h:form>

That is a contradictory... you simply never assign a global error to a specific component (the form is a component).

Simply make an error that you assign to the id of the form by using

Messages.addError(id, error);

add a <h:message for="..."> where the for contains the id of the form.


Here is how your example should look like:

import org.omnifaces.util.Messages;

// login class submit()
Messages.addError('login-form', 'Login failed');

// register class submit()
Messages.addError('register-form', 'Register failed');
 
<h:form id="login-form">    
    <h:messages globalOnly="true" for="login-from" />    
    // [...]                            
    <h:commandButton value="Login" action="#{login.submit()}"/>    
</h:form>    
<h:form id="register-form">    
    <h:messages globalOnly="true" for="register-from" />    
    // [...]                            
    <h:commandButton value="Sign up" action="#{register.submit()}"/>    
</h:form>

                    
                     
          

更多推荐

本文发布于:2023-07-22 09:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   多个   如何将   正确   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!