Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection in @FacesValidator and friends does not work always #5075

Closed
chris21k opened this issue Apr 12, 2022 · 2 comments
Closed

Injection in @FacesValidator and friends does not work always #5075

chris21k opened this issue Apr 12, 2022 · 2 comments

Comments

@chris21k
Copy link

Injection in a Facesvalidator still (at least Tomcat 10.0.18, Mojarra 3.0.2, Weld 5.0.0.CR2) does not work, althouth the class is annotated with @FacesValidator(managed = true) . All injected attributes stay null and produce NPE as a consequence.
A surprising hot fix is to use @FacesValidator(value = "xxxValidator", managed = true), i.e., to set a name is necessary instead using by default the className for that.

Please note that this is a very old bug already, see #4245, #4645 for further details. I know from my students that small things like these are very frustrating and unfortunately (and unjustly) fire too often a bad opinion about JSF among them.

@pizzi80
Copy link
Contributor

pizzi80 commented Apr 14, 2022

The bug is confirmed also with

  • Mojarra 3.0.2
  • Weld 4.0.3 (which should be the right version for Mojarra 3.x)
  • Hibernate Validator 7.0.4 (optional)

Injection does not work even declaring the name of validator
@FacesValidator(value = "xxxValidator", managed = true)

to solve I use one the following workaround:

Alternative 1: use manual injection inside the validate method

BeanService beanService = CDI.current().select(BeanService.class).get();

Alternative 2: use @ApplicationScoped if the validator is "stateless"

@ApplicationScoped
@FacesValidator(value = "CdiValidator")
public class CdiValidator implements Validator<String> {

  @Inject BeanService beanService;
  
  @Override
  public void validate(FacesContext context, UIComponent component, String value) throws ValidatorException {
  
       // ... for example ...
       beanService.findByEmail( value ) 
  
  }


}

where BeanService is like:

@ApplicationScoped
public class BeanService {

 public User findByEmail( String email ) {
    // ... 
 }

}


@BalusC
Copy link
Contributor

BalusC commented May 29, 2022

Injection in a Facesvalidator still (at least Tomcat 10.0.18, Mojarra 3.0.2, Weld 5.0.0.CR2) does not work, althouth the class is annotated with @FacesValidator(managed = true) . All injected attributes stay null and produce NPE as a consequence.

Reproduced.

A surprising hot fix is to use @FacesValidator(value = "xxxValidator", managed = true), i.e., to set a name is necessary instead using by default the className for that.

That's indeed expected. Technical explanation is that the Mojarra implementation explicitly searches for an instance literally annotated as such. I.e. <f:validator validatorId="fooValidator"> explicitly searches for @FacesValidator(value = "fooValidator", managed = true). It does not have any fallback which searches for any @FacesValidator(managed = true) after which the classname is filtered down to match the default validator ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants