简体   繁体   English

需要交互/验证的表单中的复合组件

[英]Composite-Component within a form that requires interaction/validation

A quick background : I have put a captcha using the primefaces custom component on my website. 快速背景:我已经在我的网站上使用primefaces自定义组件放置了一个验证码。 People in charge don't like it as it is too difficult to use and clients are/were complaining. 负责人不喜欢它,因为它太难使用并且客户在抱怨。 I decided I wanted to create a simple component (ie: 4 + 9 = and user inputs the answer) to avoid a bit of spam. 我决定要创建一个简单的组件(即:4 + 9 =并由用户输入答案)以避免产生一些垃圾邮件。 No need to have an image display the question, just using simple text. 无需使用图像就可以显示问题,只需使用简单的文本即可。 This got me looking into custom components and composite component ( from this article and this one ). 这使我开始研究自定义组件和复合组件(来自本文本文 )。

Now, the question is not so much about a makeshift basic "captcha style validation". 现在,问题不仅仅在于临时的基本“验证码样式验证”。 It's more about a composite component and backing bean combo. 它更多地是关于复合组件和支持bean组合的。

What I would do is create a backing bean in this style : 我要做的就是创建这种样式的支持bean:

<cc:interface>

    <cc:attribute name="label" />
<!-- edited -->
    <cc:attribute name="required" />
<cc:attribute name="ident" />

</cc:interface>


<cc:implementation>

    <h:panelGrid columns="3">
        <h:outputText value="#{captcha.text}"/>
        <h:inputText id="#{cc.attrs.ident}" value="#{captcha.entered}" validator="#{captcha.validate}" required="#{cc.attrs.required eq 'true'}" label="#{cc.attrs.label}" />
        <h:message for="captchaAnswer" />
    </h:panelGrid>

    <h:inputHidden value="#{captcha.value}" />

</cc:implementation>

And then I'd like to use this component in this manner : 然后,我想以这种方式使用此组件:

<h:form>
    ...
    <tr>
        <td>
            <my:captcha label="Captcha" ident="captcha" required="true"/> <!-- added after suggested comment -->
            <br/>
            <h:message for="captcha" class="error"/>
        </td>
    </tr>
    <tr>
        <td colspan="3" class="center">
            <h:commandButton class="button" value="#{msg['contact.label.send']}" action="#{contact.send}" >
        </h:commandButton>
        </td>
    </tr>
    ...
</h:form>

How can I make sure that on submit I can check that my {#captcha.entered} value is the required value and if not returns a validation message on the form and prevents it from being submitted? 如何确保在提交时可以检查我的{#captcha.entered}值是否为必需值,如果没有,则在表单上返回验证消息并阻止其提交?

captcha backing bean would be simple and have values : text , answer , and, entered and a simple function to check if answer == entered . captcha支持Bean很简单,并具有以下值: textanswer和, entered还有一个简单的函数来检查是否answer == entered

EDIT : (Attempt #1) custom validator would look as follow 编辑:(尝试#1)自定义验证器将如下所示

public void validate(FacesContext context, UIComponent toValidate, Object value) {

    System.out.println("validating");

    String input = (String) value;

    //check to see if input is an integer
    //if not we know right away that the value is not good
    if(StringUtils.isNumeric(input)) {
        //if the input is numeric, convert to an integer
        int intValue = new Integer(input).intValue();
        if (intValue != answer) {
            ((UIInput) toValidate).setValid(false);

            FacesMessage message = new FacesMessage("Not a match!!");
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
}

In this instance the validator does not even get called and I don't get an error message. 在这种情况下,验证器甚至不会被调用,并且我也不会收到错误消息。

Edit #2 编辑#2

After a bit of working and hints from comments I got this working. 经过一些工作和评论提示,我开始工作了。 To get the h:message working I needed to add the attribute ident instead of id . 为了使h:message工作,我需要添加属性ident而不是id If not I had to reference it as so : <h:message for="captcha:captcha" /> which was not the desired outcome. 如果没有,我必须这样引用它: <h:message for="captcha:captcha" />这不是理想的结果。

The primary issue with this question was that I couldn't get a reference. 这个问题的主要问题是我找不到参考。

By adding the attribute ident instead of id I got this to work. 通过添加属性ident而不是id我可以使用它。 Please refer to edit #2 in question. 请参考相关的修改#2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM