Browser always sends the "noSelection" value of the Grails <g:select> tag library - grails

I have this select input in my gsp:
<g:select id="whitelistId" name="whitelistId" noSelection="${['nx':'-Select whitelist-']}" from="${Whitelist.list()}" optionValue="description" optionKey="id" />
Even if I change to another item in the dropdown, "nx" value is being sent as params value. If I omit noSelection attribute, it works as expected.
This is the generated html code when "My whitelist" item is selected:
<select id="whitelistId" name="whitelistId">
<option value="nx">-Select whitelist-</option>
<option value="6118854">My whitelist</option>
</select>
I'm using Grails 2.2.0
Any tips?
Thanks

ref-doc states:
Typically this will be blank - but you can also use 'null' in the case that you're passing the ID of an object
so, it's better to use
noSelection="${['':'-Select whitelist-']}"
or
noSelection="${[null:'-Select whitelist-']}"

Related

PrimeFaces selectOneMenu have null new value in both valueChangeListener and ajax listener

I've got a problem with PrimeFaces. Here is my XHTML code :
<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
update=":contentform :popup">
<f:selectItems value="#{gCnewObjectBean.branche}" />
<p:ajax update=":contentform :popup"
listener="#{gCnewObjectBean.brancheChangedListener}" />
</p:selectOneMenu>
And here is my bean code :
public void brancheChangedListener(ValueChangeEvent event) {
Integer branche = (Integer) event.getNewValue();
// do populate the second select menu based on this value
}
public void brancheChangedListener(AjaxBehaviorEvent event) {
SelectOneMenu selectMenu = (SelectOneMenu) event.getSource();
Integer branche = (Integer) selectMenu.getSubmittedValue();
// do populate the second select menu based on this value
}
The HTML code generated is a bit strange with primefaces, the HTML select component is hidden. But if I see it with firebug, it looks like this :
<select id="branche_input" name="branche_input">
<option value="0"></option>
<option value="1">Prestations prévoyance professionnelle</option>
<option value="2">Prestations prévoyance privée</option>
<option value="3">Prestations PRIRENT</option>
<option value="4">Gestion prévoyance professionnelle</option>
</select>
This code is a way to test the two solutions (valueChangeListener and the f:ajax) described by BalusC on this post.
When I do change the value in the select menu, both listener are triggered, first the valueChangeEvent, then the AjaxBehaviorEvent, which is perfectly what I suspected based on BalusC post. But both bean method do get a "null" for the new value (with debugger, I can see that the old value in the ValueChangeEvent is "0", which is correct according to my selectItems).
Any clue to solve this ? I really dont get it, I saw the code I'm using several time on this site...
Technical info : JBoss EAP 6.0, Mojarra 2.1.7-jboss, PrimeFaces 4.0
Edit : I was wondering if it was a bug in the jsf-impl library embedded in JBoss. I updated to Mojarra 2.1.18-jboss, but the problem is strictly the same : a null value is placed into my grille object by the ajax listener and a null value is also returned by the getNewValue() object of the valueChangeEvent.
First of all, i think there is a problem with your f:selectItems tag. Try to set the itemValue and itemLabel attributes for it: (I think the gCnewObjectBean.branche is not a integerList,is it?)
<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
update=":contentform :popup">
<f:selectItems value="#{gCnewObjectBean.brancheList}" var="item"
itemValue="#{item.branche}" itemLabel="#{item.label}"/>
<p:ajax update=":contentform :popup"
listener="#{gCnewObjectBean.brancheChangedListener}" />
Your attempt to get the selected value with the getSubmittedValue() method can't work. getSubmittedValue is not returning the value you are expecting:
See Why can I not get the submitted value from component binding?
If this is not the solution to your problem, you can try to put a p:messages tag in your jsf page. Probably there are errors which does not show up in the console.

All radioButtons are false at the view

#Html.RadioButton("smth",true)
This row at the page looks like UNCHECKED radioButton. Why?
You should use
HtmlHelper.RadioButton(string name, object value, bool isChecked)
extension method.
First argument is the name of the form field which is input field generated by html helper.
Second argument is the value of input element. If this radio is selected when the postback to server happens, this value is used.
Third argument is what you are looking for. If it is true it makes radio button selected.
For instance,
#Html.RadioButton("Name", "Value" ,true)
would generate an input element which looks like following,
<input checked="checked" id="Name" name="Name" type="radio" value="Value" />
You need to use something of the form
#Html.RadioButton(id,value,checked (bool true/false))
So
#Html.RadioButton("A","B",true)
For example would produce:
<input checked="checked" id="A" name="A" type="radio" value="B" />
The documentation for this is here
In your previous query, you got this answer.
You asked the same query in it's comment section. The problem was that the second line of code was missing one parameter.
Please check below details....
Parameter Details
Razor Syntax
#Html.RadioButton("smth", "smth", true)
#Html.RadioButtonFor( m => m.Prop, true, new { id = "rdBtn" } )

Grails check checkbox with value same as variable

Hi I have this value that I passed to the gsp
${d.causalOrganism}
I have this three checkboxes and I want one of them to be checked based on the above value. How can I do so? I tried but doesn't work.
<label for="causalOrganism">Causal Organism:</label>
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)"/>
Bacteria<input type="checkbox" value="Bacteria" name="causalOrganism" id="causalOrganism2" onclick="checkOrganism(this.id)"/>
Virus<input type="checkbox" value="Virus" name="causalOrganism" id="causalOrganism3" onclick="checkOrganism(this.id)" />
Add the checked attribute to your checkbox based on your desired condition:
...
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)" ${(d.causalOrganism == 'causalOrganism1') ? "checked='checked'" : ''}/>
...
You should think about using the grails checkBox. With this you could use the checked attribute directly.

jQueryMobile: Multiple-Select sent all values to server

I want to use a multiple-select field in jQuery-Mobile. However only the last selected value is sent to the server. This is my Code:
<select name="myValues"
data-native-menu="false" data-mini="true" multiple="multiple">
<option>Select one or more</option>
<?php
foreach ($propPosValues AS $posValue) {
<option value="<?= $field['Id'] ?>"><?= $field['Value'] ?></option>
<?php } ?>
</select>
Don't bother about the php-code. It actually works. The UI shows a multiple-selects that opens in another window. But only the last selected-Value is sent to the server. But I obviously want all of them.
Any suggestions?
The name-tag needs to be name="myValues[]".
The brackets "[]" indicate, that this is an array of values.

Grails <g:if> in <g:select>

I have this <g:select> in a .gsp file. But unlike any ordinary <g:select>'s this one would have the attribute disabled="" if a certain condition is met.
Following the code:
<g:select name="test"
from="${["foo1","foo2"]}"
<g:if test="${true}">disabled=""</g:if> />
It returned an error: Grails tag [g:select] was not closed
But when I change it into this:
<g:select name="test"
from="${["mu1","mu2","mu3"]}"
${ if(true) { println "disabled=\"\"" } }/>
It returned this error: Attribute value must be quoted.
Both of the error message are under the exception, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
The question is how could we make this work? Is there a possible answer without using a custom TagLib?
The GSP form field tags treat disabled as a boolean property, so you can say
<g:select .... disabled="${true}" />
Generally you should be able to use any expression under the usual Groovy-truth rules but I believe it makes a special case for the strings "true" and "false" (the latter would normally be considered true under Groovy-truth rules as a non-empty string). If in doubt you can always say
disabled="${(someExpression) as boolean}"
No need to use the println, try this
<g:select .... ${(conditional)?"disabled":""} ... />
<g:select disabled="${true}"...
is fine but when you submit and it is a required field the value will not be submitted so use this jQuery code to enable the field when pressing the submit button
$(function() {
$('form').on('submit', function() {
$(this).find(':disabled').removeAttr('disabled');
});
});

Resources