Strange behaviour of selectOneMenu

UI Components for JSF
Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

20 Jul 2021, 09:26

I have this select:

Code: Select all

<p:selectOneMenu  value="#{bean.val}">
    <f:selectItem itemValue="X" itemLabel="Select" />
    <f:selectItems value="#{bean.getVals()}" />
    <p:ajax update="wrapper" />
</p:selectOneMenu>
And this is the wrapper it updates:

Code: Select all

<p:panel id="wrapper">
    <p:panel rendered="#{bean.val == 'A' or bean.val == 'B'}">
        <!-- insert your code here -->
    </p:panel> 
</p:panel>
At the start, it's all ok. The wrapper is hidden.

If I select 'A' and then 'C', for example, the wrapper disappear. BUT, if I select 'A' or 'B' and 'X' again (the first "choice", Select), the wrapper does NOT disappear!

I have put a breakpoint inside the setter of bean.val. The setter in invoked for all the choices BUT NOT for the first one!

When I put Select as empty string, it worked. But I can't put it as empty string anymore.
I added a custom validator, that checked if the value is an empty string. It simply did not work. So I had to put a bogus value for Select.

This is the old validator:

Code: Select all

@FacesValidator(value="requiredString")
public class RequiredString implements Validator {
    protected MessageUtil messageUtil = new MessageUtil();
    
    @Override
    public void validate(
        FacesContext context, 
        UIComponent component, 
        Object value
    ) throws ValidatorException {
        String val = (String) value;
        
        if (val == null || val.trim().isEmpty()) {
            FacesMessage msg = this.messageUtil.getDefaultValidationError();
            throw new ValidatorException(msg);
        }
    }
}

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

20 Jul 2021, 14:26

If I had to guess its a "Converter" issue.

<f:selectItem itemValue="X" itemLabel="Select" />

Is not the same Type of object being converted as...

<f:selectItems value="#{bean.getVals()}" />

So its not being converted and set to your Bean.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

20 Jul 2021, 14:43

No, I also tried to remove it from jsf and put into the method :-(

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

20 Jul 2021, 14:59

If you create a PrimeFaces Test reproducer I can debug it. But most likely its something with your setup. Remove all other factors like your Validator etc.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

20 Jul 2021, 15:02

Melloware wrote:
20 Jul 2021, 14:59
If you create a PrimeFaces Test reproducer I can debug it.
How can I do it?
Melloware wrote:
20 Jul 2021, 14:59
But most likely its something with your setup. Remove all other factors like your Validator etc.
I can't. The Validator is *essential*. Anyway, I'll try to remove piece by piece and see when it works.

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

20 Jul 2021, 15:23

PFTest is here: https://github.com/primefaces/primefaces-test

Its a small standalone project that runs with Jetty that allows you to boil problems down to the simplest example for developers can run and debug.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

20 Jul 2021, 17:04

I removed the validator and it works... but I need it!

This is the validator:

Code: Select all

@FacesValidator(value="requiredSelect")
public class RequiredSelect implements Validator {
    protected MessageUtil messageUtil = new MessageUtil();
    
    @Override
    public void validate(
        FacesContext context, 
        UIComponent component, 
        Object value
    ) throws ValidatorException {
        String val = (String) value;
        
        if ("X".equals(val)) {
            HtmlSelectOneMenu htmlInput = (HtmlSelectOneMenu) component;
            String classes = htmlInput.getStyleClass();
            String newClasses = classes + " ui-state-error";
            htmlInput.setStyleClass(newClasses);
            
            FacesMessage msg = this.messageUtil.getDefaultValidationError();
            throw new ValidatorException(msg);
        }
    }
}

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

20 Jul 2021, 17:08

OK now you know the root of your problem. You will have to debug it.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

20 Jul 2021, 17:52

I don't know why! I see that simply the setter is not invoked for the first element. But Why???? I have no clue. If I have had an exception at least I have a stack trace. Here I have NOTHING :D

Marco Sulla
Posts: 25
Joined: 16 Jun 2021, 16:48

21 Jul 2021, 10:15

I noticed that it's not that the setter is not invoked.

If I select another option, and then again the "Select" ("X"), option, the old value selected is passed again to the setter!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 38 guests