InputText PrimeFaces不应用maxlength
我正在使用PrimeFaces 3.4和PrimeFaces Mobile 0.9.3。我在inputText属性中指定了maxlength,但它没有在HTML中呈现。我的代码:
I'm using PrimeFaces 3.4 with PrimeFaces Mobile 0.9.3. I specified maxlength in the inputText attribute, but it is not being rendered across in the HTML. My code:
<p:inputText id="price" value="#{bean.price}" styleClass="r-align"
type="number" maxlength="9" validator="priceValidator"/>
后来我发现当我从标签中删除type属性时,max length有效。
有谁知道为什么会这样?
Later I found that when I remove the "type" attribute from the tag, max length works. Does anyone know why this is the case?
那只是因为 maxlength HTML5 属性c>< input type =number>
元素,因此可以合理地假设PrimeFaces渲染器不会发出它。
That's simply because maxlength
attribute is not supported on the HTML5 <input type="number">
element, so it's reasonable to assume that PrimeFaces renderer won't emit it.
相反,您应该使用 min
和 max
属性。从理论上讲,你应该设置为
Instead, you should be using min
and max
attributes. Theoretically, you should be set with
<p:inputText type="number" max="999999999" />
然而,这对我不起作用。它没有呈现 max
(也不是 min
)属性。这反过来可能是PrimeFaces组件的疏忽。最好的办法是向PrimeFaces人员报告这个问题。
However, this didn't work for me. It didn't render the max
(nor the min
) attribute altogether. This is in turn likely an oversight in the PrimeFaces component. Your best bet is to report it as an issue to PrimeFaces guys.
同时,您可以通过提供这样的自定义渲染器来解决这个问题,它基本上会添加 min
和 max
属性到pass thru属性列表:
In the meanwhile, you can work this around by providing a custom renderer like this which basically adds the min
and max
attributes to the list of pass thru attributes:
package com.example;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.component.inputtext.InputTextRenderer;
public class MyInputTextRenderer extends InputTextRenderer {
@Override
protected void renderPassThruAttributes(FacesContext facesContext, UIComponent component, String[] attrs) throws IOException {
String[] newAttrs = new String[attrs.length + 2];
System.arraycopy(attrs, 0, newAttrs, 0, attrs.length);
newAttrs[newAttrs.length - 2] = "min";
newAttrs[newAttrs.length - 1] = "max";
super.renderPassThruAttributes(facesContext, component, newAttrs);
}
}
您可以通过以下方式运行注册如下:
which you can get to run by registering it as follows:
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.InputTextRenderer</renderer-type>
<renderer-class>com.example.MyInputTextRenderer</renderer-class>
</renderer>
</render-kit>
请注意,在即将推出的JSF 2.2中,将自然地支持传递自定义JSF组件属性,其他HTML5 数据 - *
属性*。
Note that passing through custom JSF component attributes will be natively supported in the upcoming JSF 2.2, which allows among others HTML5 data-*
attribute freedom.
- How to let JSF pass through HTML attributes