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

b:selectOneMenu null itemValue #939

Closed
chongma opened this issue May 8, 2018 · 8 comments
Closed

b:selectOneMenu null itemValue #939

chongma opened this issue May 8, 2018 · 8 comments
Assignees
Labels
Milestone

Comments

@chongma
Copy link
Collaborator

chongma commented May 8, 2018

consider the following h:selectOneMenu code snippet:

<h:selectOneMenu value="#{myBean.value}">
	<f:selectItem itemValue="#{null}" itemLabel="Not selected" />
	<f:selectItem itemValue="choice1" itemLabel="choice1" />
	<f:selectItem itemValue="choice2" itemLabel="choice2" />
</h:selectOneMenu>

when Not selected is selected then myBean.value equals null

<b:selectOneMenu label="Nice formatting" value="#{myBean.value}">
	<f:selectItem itemValue="#{null}" itemLabel="Not selected" />
	<f:selectItem itemValue="choice1" itemLabel="choice1" />
	<f:selectItem itemValue="choice2" itemLabel="choice2" />
</b:selectOneMenu>

whereas with b:selectOneMenu when Not selected is selected then myBean.value equals Not selected

@dguna
Copy link

dguna commented May 30, 2018

I am not sure this is the correct way to do. Selectonemenu does not support null value. If it is empty then the bean has to convert as null.

@stephanrauh
Copy link
Collaborator

It was a deliberate choice to use the label if the value is missing, but probably it wasn't a wise choice. I've removed it because we've never documented the feature. Still, it's a breaking change.

Passing null values to the bean is difficult. During my test the null value has always been replaced by an empty string. To avoid that, you probably have to set the parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true.

@chongma Can you review and test my changes, please?

@stephanrauh stephanrauh self-assigned this May 31, 2018
@stephanrauh stephanrauh added this to the v1.5.0 milestone May 31, 2018
@dguna
Copy link

dguna commented May 31, 2018

Null is not an value. It means empty. JSF treats everything as string and the converters to the rest of the work to convert it to objects. There is no "null" object. This will fail always.

@chongma
Copy link
Collaborator Author

chongma commented May 31, 2018

I have been passing nulls for a long time with (usually) no problem. But looking at this answer by @BalusC it seems that @dguna is correct and that the preferred method is to pass an empty string. Not sure why null always worked for me before https://stackoverflow.com/questions/17052503/using-a-please-select-fselectitem-with-null-empty-value-inside-a-pselectonem?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

@stephanrauh
Copy link
Collaborator

@dguna You are right... mostly. Have a look at Bauke's well-researched article.

@stephanrauh
Copy link
Collaborator

I'll close this ticket. If anyone of your finds a bug in my bugfix, don't hesitate to re-open the ticket.

@dguna
Copy link

dguna commented May 31, 2018

Don"t close it yet. You have to give an option as f:selectitems noSelectionOption="true", it means empty string and if required=true, it should not be submitted. JSF automatically converts empty as null, if I am correct. Please look at the Primefaces selectonemenu.

@stephanrauh
Copy link
Collaborator

There are several references to noSelectionOption in our source code, so I assume we've already implemented it. For example, have a look at this one:

private SelectItemAndComponent determineSelectedItem(FacesContext context, SelectOneMenu menu, List<SelectItemAndComponent> items, Converter converter) {
		Object submittedValue = menu.getSubmittedValue();
		Object selectedOption;
		if (submittedValue != null) {
			selectedOption = submittedValue;
		} else {
			selectedOption = menu.getValue();
		}

		for (int index = 0; index < items.size(); index++) {
			SelectItemAndComponent option = items.get(index);

                        
			if (option.getSelectItem().isNoSelectionOption()) continue;


			
			Object itemValue = option.getSelectItem().getValue();
			String itemValueAsString = getOptionAsString(context, menu, itemValue, converter);

			Object optionValue;
			if (submittedValue != null) {
				optionValue = itemValueAsString;
			} else {
				optionValue = itemValue;
			}

			if (itemValue != null) {
				if (isSelected(context, menu, selectedOption, optionValue, converter)) {
					return option;
				}
			} 
		}
		return null;
	}
	

@stephanrauh stephanrauh modified the milestones: v1.5.0, v1.3.0 Oct 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants