Search code examples
javajsf-2richfaces

JSF 2.1 validateBean always called before preRenderView event


Im trying to use dynamic validationGroups

<h:inputText id="id" value="#{bean.char}" maxlength="8" alt="#{bean.displayName}">
        <f:validateBean
                validationGroups="#{bean.validationGroup}" />
</h:inputText>

However the #{bean.validationGroup} is always called before the

<f:event type="preRenderView" listener="#{bean.initView}" />

Is this a bug in Myfaces? I need to have #{bean.validationGroup} called after the prerenderview because preRender loads data from the db and validationGroups will be different. Im using Myfaces 2.1.5 with richfaces 4.1.


Solution

  • This is indeed specified behaviour. The <f:xxx> tags run during view build time. If you like to initialize properties before the view is been built, then you need to do the job in the (post)constructor method of the bean. The pre render view event runs right before the view is to be rendered, but long after the view is been built.

    Remove the <f:event> altogether and make the initView a @PostConstruct method instead.

    @PostConstruct
    public void initView() {
        // ...
    }
    

    It will run directly after bean's construction and finishing of all dependency injections like @ManagedProperty, @Inject, @EJB and so on.