f:validateLength 的 Primefaces Dynaform 奇怪行为

Primefaces Dynaform Strange Behavior With f:validateLength

我目前正在使用 Primefaces 8.0 和 Primefaces Extension 8.0 创建动态表单。我想使用 f:validateLength 在用户输入中对具有最小和最大长度的 phone 数字进行长度验证,但不知何故验证总是失败,说长度大于允许的长度,即使我将最大值设置为 10 和我只输入了 1 个字符。这是我的代码

xhtml:

<h:form prependId="false">
            <p:messages/>
            <pe:dynaForm id="df" 
                         value="#{testMBean.modelz}" var="data" 
                         class="ui-fluid" style="width: 100%;" varContainerId="dfid">
                <pe:dynaFormControl id="dfc-text" type="Text" style="white-space: normal;">
                    <p:panelGrid id="pg-txt" columns="3" layout="grid" class="no-padding no-border"
                                 styleClass="ui-panelgrid-blank form-group"
                                 columnClasses="ui-grid-col-4,ui-grid-col-8,ui-grid-col-2">
                        <p:outputLabel for="@next" 
                                       value="#{data.labelEn}"/>
                        <p:inputText id="txt" value="#{data.value}" required="false">
                            <f:validateLength for="@previous" minimum="#{data.minLength}" maximum="#{data.maxLength}"/>
                        </p:inputText>
                        <p:outputLabel value="#{data.minLength} - #{data.maxLength}"/>
                    </p:panelGrid>
                </pe:dynaFormControl>
            </pe:dynaForm>
            <p:commandButton process="@form" update="@form"/>
</h:form>

TestMBean:

private DynaFormModel modelz;

modelz = new DynaFormModel();
DynaFormRow row = modelz.createRegularRow();
DynamicModel dm = new DynamicModel();
dm.setMinLength(0);
dm.setMaxLength(10);
dm.setLabelEn("Test");
row.addControl(dm, "Text");
dm = new DynamicModel();
dm.setMinLength(1);
dm.setMaxLength(20);
dm.setLabelEn("Test2");
row.addControl(dm, "Text");

动态模型:

@Getter
@Setter
@ToString
public class DynamicModel implements Serializable {
    private String labelEn;
    private Object value;
    private Integer minLength;
    private Integer maxLength;
}

结果:

据说最大允许值为 0,但如您所见,我显示了 minLength 和 maxLength 的值,它显示 minLength 为 0,maxLength 为 10。所以我检查了源并得到了这个:

我是不是做错了什么?谁能指出我哪里做错了。 谢谢。

我使用自定义验证器解决了这个问题,该验证器使用 f:attribute 标签扩展 javax.faces.validator.LengthValidator 来设置最小值和最大值。

创建 TextValidator 扩展 javax.faces.validator.LengthValidator 以拦截属性:

public class TextValidator extends LengthValidator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {
        if (value != null && !value.toString().trim().isEmpty()) {
            Object minimum = component.getAttributes().get("minimum");
            Object maximum = component.getAttributes().get("maximum");
            setMinimum(minimum != null ? Integer.valueOf(minimum.toString()) : 0);
            setMaximum(maximum != null ? Integer.valueOf(maximum.toString()) : 0);
        }
        super.validate(context, component, value);
    }

}

TestMBean:

TextValidator textValidator = new TextValidator();
modelz = new DynaFormModel();
DynaFormRow row = modelz.createRegularRow();
DynamicModel dm = new DynamicModel();
dm.setMinLength(0);
dm.setMaxLength(10);
dm.setLabelEn("Test");
row.addControl(dm, "Text");
dm = new DynamicModel();
dm.setMinLength(2);
dm.setMaxLength(20);
dm.setLabelEn("Test2");
row.addControl(dm, "Text2");

然后将textValidator绑定到xhtml中的f:validator标签:

<h:form prependId="false">
        <p:messages/>
        <pe:dynaForm id="df" 
                     value="#{testMBean.modelz}" var="data" 
                     class="ui-fluid" style="width: 100%;" varContainerId="dfid">
            <pe:dynaFormControl id="dfc-text" type="Text" style="white-space: normal;">
                <p:panelGrid id="pg-txt" columns="3" layout="grid" class="no-padding no-border"
                             styleClass="ui-panelgrid-blank form-group"
                             columnClasses="ui-grid-col-4,ui-grid-col-8,ui-grid-col-2">
                    <p:outputLabel for="@next" 
                                   value="#{data.labelEn}"/>
                    <p:inputText id="txt" value="#{data.value}" required="false">
                        <f:validator binding="#{testMBean.textValidator}"/>
                        <f:attribute name="minimum" value="#{data.minLength}"/>
                        <f:attribute name="maximum" value="#{data.maxLength}"/>
                    </p:inputText>
                    <p:outputLabel value="#{data.minLength} - #{data.maxLength}"/>
                </p:panelGrid>
            </pe:dynaFormControl>
            <pe:dynaFormControl id="dfc-text2" type="Text2" style="white-space: normal;">
                <p:panelGrid id="pg-txt2" columns="3" layout="grid" class="no-padding no-border"
                             styleClass="ui-panelgrid-blank form-group"
                             columnClasses="ui-grid-col-4,ui-grid-col-8,ui-grid-col-2">
                    <p:outputLabel for="@next" 
                                   value="#{data.labelEn}"/>
                    <p:inputText id="txt2" value="#{data.value}" required="false">
                        <f:validator binding="#{testMBean.textValidator}"/>
                        <f:attribute name="minimum" value="#{data.minLength}"/>
                        <f:attribute name="maximum" value="#{data.maxLength}"/>
                    </p:inputText>
                    <p:outputLabel value="#{data.minLength} - #{data.maxLength}"/>
                </p:panelGrid>
            </pe:dynaFormControl>
        </pe:dynaForm>
        <p:commandButton process="@form" update="@form"/>
    </h:form>

如果验证失败,输出如下:

下面是值是否通过验证:

Thanks to this answer