简体   繁体   English

根据Bean Validation @Size自动设置h:inputText maxlength

[英]Automatically set h:inputText maxlength based on Bean Validation @Size

I use JSF 2 and Hibernate Validator 5.1 in a web application. 我在Web应用程序中使用JSF 2和Hibernate Validator 5.1。

I use the annotations like @Size or @NotNull in my beans to validate automatically my HTML forms. 我在@NotNull中使用@Size@NotNull类的注释来自动验证HTML表单。

It works perfectly but I would like to know if there is a way to automatically limit the number of characters typed in an input text (ie set the attribute maxlength in HTML) based on the @Size annotation. 它可以完美工作,但是我想知道是否有一种方法可以基于@Size注释自动限制输入文本中键入的字符数(即,在HTML中设置属性maxlength)。

Use maxlength attribute of <h:inputText> 使用<h:inputText>的maxlength属性

You can also use f:validate - 您也可以使用f:validate-

<h:inputText id="Username" value="#{model.username}" 
                 required="true"
                label="UserName" >
                <f:validateLength minimum="5" maximum="10" />
            </h:inputText>

HTML5 + JSF 2.2+ HTML5 + JSF 2.2+

If you're using HTML5 and JSF 2.2+, specify it as a passthrough attribute . 如果您使用的是HTML5和JSF 2.2+,则将其指定为passthrough属性

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">

<h:inputTextarea value="#{bean.text}" a:maxlength="2000" />

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

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