简体   繁体   English

如何使用翻译器和/或验证器制作Tapestry 4中所需的String字段?

[英]How can I use a translator and/or validator to make a String field required in Tapestry 4?

I'm using Tapestry 4. 我正在使用Tapestry 4。

I have several TextFields whose values get passed into Strings in the page class, and they work great as long as there is some content in the fields. 我有几个TextFields,它们的值都传递给page类中的Strings,只要字段中有一些内容,它们就可以很好地工作。 Most of them are optional, so I believe I can use the StringTranslator with empty= in that case, but for a couple of fields for which a value is required, I'm having a hard time getting validation to work. 它们中的大多数是可选的,因此我相信在那种情况下我可以将StringTranslatorempty=一起使用,但是对于需要为其提供值的几个字段,我很难使验证工作。

I expected a simple required validator to work: 我期望一个简单的required验证器可以工作:

<component id="myRequiredField" type="TextField">
    <binding name="value" value="ognl:stringValue" />
    <binding name="validators" value="validators:required" />
</component>

Failing that, I expected minLength to work: 失败了,我希望minLength可以工作:

<component id="myRequiredField" type="TextField">
    <binding name="value" value="ognl:stringValue" />
    <binding name="validators" value="validators:required,minLength=1" />
</component>

Both attempts at validation allow the value as retrieved with getStringValue() to be null upon form submission. 两次验证尝试都允许在提交表单时使用getStringValue()检索的值为null。 My Form and Submit components look like: 我的FormSubmit组件如下所示:

<component id="myUpdateForm" type="Form">
    <binding name="delegate" value="beans.validationDelegate" />
</component>
<component id="submitUpdate" type="Submit">
    <binding name="action" value="listener:doUpdate" />
</component>

It turns out that the validation was working, but I wasn't checking whether my validation delegate had errors before operating on the incoming data. 事实证明验证工作正常,但是在处理传入数据之前,我没有检查验证委托是否有错误。 The following seems to be the correct approach to take in any listener that depends on validation, given the setup as listed in the question: 考虑到问题中列出的设置,以下似乎是采用任何依赖于验证的侦听器的正确方法:

@Bean
public abstract ValidationDelegate getValidationDelegate();

public void doUpdate() {    
    if (!getValidationDelegate().getHasErrors()) {
        // business logic
    }
}

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

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