Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of OmniFaces Param.validatorAttributes

Tags:

jsf

omnifaces

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.

like image 569
darefilz Avatar asked Sep 11 '19 16:09

darefilz


1 Answers

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.

like image 107
BalusC Avatar answered Nov 16 '22 07:11

BalusC