Foros de discusión

Initialization issue with alloy UI checkboxes

Piña Kanpokaldean, modificado hace 9 años.

Initialization issue with alloy UI checkboxes

Junior Member Mensajes: 79 Fecha de incorporación: 14/04/12 Mensajes recientes
Hi,

I'm running into trouble with alloy UI checkboxes, and I could use a hand. The basic problem is that, when I try to retrieve values in java after submiting the form, I apparently get all values regardless of whether I've selected the checkbox or not.

I have also noticed that alloy UI checkboxes apparently switch the value parameter between the actual value and "false", according to whether they are checked or not. Through this, I can handle single checkboxes without issue. However, regardless of me setting the checkbox's "checked" attribute to true or false, the value is always initialized to the value I enter; which means that an unchecked . Therefore, when I get the values of the checkbox group name, I get them all and they all get the value they were defined. Under those circumstances, I have no way of knowing which ones have actually been checked by the user. Checking which ones have been set to false and consider those unchecked works, but only when they have been actively unchecked by the user and not when they where unchecked to begin with.

At this point, I no longer know if this is a bug or I just don't know how to use the checkboxes. I believe I can probably figure a workaround out by naming each one separately and then looping through them as if they were a bunch of independent checkboxes, but I don't think that's the correct way of doing things and I'd rather avoid doing this if I can.

In case it helps, this is my code. I must note that, due to a design that hides the normal checkbox and shows a custom box through CSS, I can't use the standard aui label and I had to add a custom one myself, but this does work except for the problem I'm bringing up.

JSP side:

    <label class="width210 noMargin"><liferay-ui:message key="profile.am-interested-in-categories" /></label>         
        <div class="marginLeft240 marginTopMinus25">
        	&lt;%
						List<category> categories = CategoryLocalServiceUtil.findAllCategoriesUserIsInterestedIn(userId);
						for (Category category : CategoryLocalServiceUtil.getAllProjectCategories()) {
							boolean checked = (categories.contains(category));
							String cssId= "category" + category.getCategoryId();
					%&gt;
            <aui:input id="<%=cssId%>" type="checkbox" name="categories" checked="<%=checked%>" value="<%=category.getCategoryId()%>" label="" />  
            <label for="<%=renderResponse.getNamespace()%>category<%=category.getCategoryId()%>Checkbox">&lt;%=category.getName()%&gt;</label>  
            <br>
					&lt;%}%&gt;<br> 
        </category></div>


Java side:
	private List<category> getCategoriesFromRequest (ActionRequest request) throws Exception {
		List<category> categories = new ArrayList<category>();
		if (request.getParameterValues("categories") != null) {
			for (long categoryId : ParamUtil.getIntegerValues(request, "categories")) {
				if (categoryId != 0) {
					categories.add(CategoryLocalServiceUtil.getCategory(categoryId));
				}
			}
		}
		return categories;
	}</category></category></category>
thumbnail
Pankaj Kathiriya, modificado hace 9 años.

RE: Initialization issue with alloy UI checkboxes

Liferay Master Mensajes: 722 Fecha de incorporación: 5/08/10 Mensajes recientes
When you use aui:input with type as checkbox, it creates one hidden input field with name as same as aui:input and one input checkbox with name as "name provided in aui:input" followed by Checkbox.

For Example:

<aui:input type="checkbox" name="categories" checked="<%=checked%>" value="<%=category.getCategoryId()%>" label="" />  

above code will result into something like

<input type="hidden" name="PORTLETNAMESPACEcategories" value="CATEGORYID">  
<input type="checkbox" name="PORTLETNAMESPACEcategoriesCheckbox" value="CATEGORYID">  


So, here when you try to get all request parameter by name "categories", then it will return you all values irrespective if its selected or not.
But, if you get categoriesCheckbox request parameter values, you will get only selected values.

HTH
Piña Kanpokaldean, modificado hace 9 años.

RE: Initialization issue with alloy UI checkboxes

Junior Member Mensajes: 79 Fecha de incorporación: 14/04/12 Mensajes recientes
Yep, that was the thing. Thanks. I had seen the hidden field, I even noticed that I had to attach "Checkbox" to the name on the JSP, but for some reason I apparently completely forgot about it on the java code.

Problem solved.
thumbnail
Claudio Carlenzoli, modificado hace 6 años.

RE: Initialization issue with alloy UI checkboxes

New Member Mensajes: 12 Fecha de incorporación: 31/12/14 Mensajes recientes
I know that this post is rather old, but I'm facing similar issue trying to fix a Portlet configuration page using the preference key naming convention (based on "--" characters sequence).
In this case since when you set checked="false" both checkbox and hidden field still have same values and the only difference is that checkbox doesn't submit its value whereas hidden does (obviously!)
My problem is that the portlet preference key is managed by the hidden field...and not by the checkbox widget (the one sending parameter terminating with Checkbox suffix) since doesn't terminate with the characters sequence "--".

Unfortunately, code I'm fixing, suppose to use semantic values (not a more trivial true/false pair) and for this reason the only solution I thought was to implement a JS code in order to clean hidden input values (or better set them to false) for every intial unchecked checkboxes.
In this way portlet preferences are managed in correct way and when user check one of the initial unchecked the real semantic value is set in hidden input preserving what expected by portlet code of the application I fixed.

I'm wondering if Alloy Input Checkbox can handle the checked value keeping separated to the unchecked one...It would be an easy feature to implement giving a better flexibility to the tag.
Probably is already defined but reading documentation I was not able to find it!

Sincerely

Claudio
thumbnail
Shreya Pandey, modificado hace 6 años.

RE: Initialization issue with alloy UI checkboxes

New Member Mensajes: 2 Fecha de incorporación: 28/09/17 Mensajes recientes
I am also facing a similar issue and none of the solutions is working. Please, can anyone suggests something else?