Usage of OmniFaces Param.validatorAttributes
Asked Answered
A

1

6

I try to use the OmniFaces @Param annotation to inject a request parameter.
I also make use of its validatorClasses attribute to validate the parameter. Eventually this used validator needs a special attribute to function and I want to pass the value by setting the validatorAttributes attribute. Unfortunately I don't know how. The documentation provides a description but I just don't get it right.

Can someone help please?

Here's some code:

    @Inject
    @Param(
            name = "the_param_name",
            validatorClasses = MyFreshValidator.class,
            validatorAttributes = ?
    )
    private MyFreshClass instance;

It would be ideal to give another object of the same class to the validator.

Aviator answered 11/9, 2019 at 16:16 Comment(0)
Y
5

It's indeed slightly hidden in the showcase. If you open the CdiParamBean tab of the "Demo source code" section, then you'll find the managed bean's source code with below examples:

// Like <f:viewParam name="text2" value="#{bean.text2}" validatorMessage="..."><f:validateLength minimum="3">
@Inject @Param(
    validatorClasses = LengthValidator.class,
    validatorAttributes = @Attribute(name="minimum", value="3"),
    validatorMessage = "{1}: Value is too too small! Please enter a minimum of 3 characters.")
private String text2;

// Like <f:viewParam name="date" value="#{bean.date}" converterMessage="..."><f:convertDateTime pattern="yyyyMMdd">
@Inject @Param(
    converterClass = DateTimeConverter.class,
    converterAttributes = { @Attribute(name="pattern", value="yyyyMMdd") },
    converterMessage="{1}: \"{0}\" is not the date format we had in mind! Please use the format yyyyMMdd.")
private Date date;

Here, the @Attribute is the org.omnifaces.cdi.param.Attribute.

I'll look at improving the documentation in a future version.

Yarkand answered 11/9, 2019 at 16:36 Comment(2)
I guess it's not possible to pass a non-constant value for value (e.g. a field of the same class), right?Aviator
The value supports EL. So you could use e.g. value="#{bean.property}".Yarkand

© 2022 - 2024 — McMap. All rights reserved.