Null param value on form submit grails - grails

I have one checkbox(id, name:checkedEx) in grails gsp list and have sortableColumn where I pass checkbox value in params map. Now when I click any of the sortable column, the checkbox gets checked, as the query string gets the value localhost:8080/api-name/list.gsp?checkedEx=null (this null value sets the checkbox)
<g:checkBox name="checkedEx" id="checkedEx" value="${checkedEx}" onclick="this.form.submit();" />
<g:sortableColumn property="date" title="date" params='["checkedEx":"${checkedEx}"]' />
How to check if the value of checkedEx is null , then in sortableColumn send param checkedEx as empty.
Thanks!

You can check it on the same line like this:
<g:sortableColumn ... params='["checkedEx":"${checkedEx?checkedEx:''}"]' />
or use a
<g:if> tag and <g:set> if you want to do more than use the operator

Related

Grails default methods in controller

I am having a problem saving a domain instance in grails; The domainInstance that's passed to the default update method in my controller is NULL. The GSP page I am submitting from is not the default edit page. I have certain values from DB that need to be saved. I have the form tag defined on the page that contains the values I need to submit, as follows.
<g:form id="sampleForm" url="[resource:domainInstance, controller:'cntrlrName', action:'update']" method="PUT" >
I also have a version field which looks like this.
<g:hiddenField name="version" value="${domainInstance?.version}" />
My g:submit is as follows
<g:actionSubmit action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
Why is my domain instance null? What am I missing?
This is the common mistake one could make. The attribute id in <g:form> tag is not the id attribute of HTML tags but it is the id to use in the link for default mapping of Grails i.e.
"/$controller/$action/$id" {}
So change your tag as:
<g:form name="sampleForm" id="${domainInstance.id}" controller="cntrlrName" action="update" method="PUT">
You can pass the domainInstance but I feel it is better practice to pass the id instead of the object. Try passing the id of the domain instance and then reading the object in the controller.
<g:form name="sampleForm" action="action" controller="controller" id="${domainInstance.id}" ></g:form>
// in controller
def resource = Domain.read(params.id)
another approach could be to pass the domainInstance as a hiddenField
<g:hiddenField name="resource" value="$domainInstance" />

Grails populate g:select from database

I ran into an issue while populating domain object values on a gsp page by using a g:select tag.
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
In my controller, I have an object which gathers values from my domain. I tried writing this object in the controller both as:
def d = Demo.getAll()
and def d = Demo.list()
Both of these returned an exception while trying to use the g:select tag. The error was:
No signature of method: Demo.getAt() is applicable for argument types: (java.util.ArrayList) values: [[One, Two, Three, Four, Five]]
I was able to get my code working by simply removing the g:select tag and writing the following in my gsp:
<select name="demo" id="dm">
<option value="">Select!</option>
<g:each in="${dm}" status="i" var="dm">
<option value="${dm.id}">${dm.name}</option>
</g:each>
</select>
However, I would like to know how to make this work with the g:select tag, for my future reference.
Thanks in advance!
I see two issues in your select:
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
value shouldn't be looking at the collection for the id. I believe this is why you are getting your error. value should be whatever instance's id you've passed into your view.
optionValue doesn't required an expression.
Controller action
def someAction() {
def dm = Demo.get(params.id)
def demos = Demo.list()
[dm: dm, demos: demos]
}
view
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${demos}"
optionValue="type"/>

asp.net mvc and valid xhtml?

For some reason an html helper is outputting this html which doesnt validate.
the validator tells me
There is no attribute "Length"
<%= Html.CheckBox("Medicamentos", Model.Medicamentos) %>
is outputting
<input type="checkbox" value="true" name="Medicamentos" id="Medicamentos" checked="checked" length="4">
I assume that it's matching the signature that takes a string and an object since I don't know what Model.Medicamentos is. In that case it takes the properties of the object and turns them into attributes on the element. I suspect that you simply want to use the Checked attribute on the Model property specified as the default value of the checkbox, i.e.,
<%= Html.CheckBox( "Medicamentos", Model.Medicamentos.Checked ) %>
In, which case, assuming that Checked is boolean it will match the correct method signature on the helper extension.

Grails form data binding when another domain object is referenced in the form

How do I get Grails data binding to correctly bind referenced objects (such as Country in the example below)?
Given the following two Grails domain classes ..
class User {
String username
Country country
}
class Country {
String name
}
.. and the following HTML form ..
<g:form>
<g:textField name="user.username" value="${user.username}" />
<g:select name="user.country" from="${Country.list()}" optionKey="id" />
</g:form>
.. and the following code in the corresponding action ..
User user = new User(params["user"])
.. I would have hoped that user.username and user.country would get bind. However, it appears as if username.username gets bind, whereas user.country is not. What is the correct syntax to bind referenced objects (user.country in this example)?
The binding of the "country" property starts working if ..
<g:select name="user.country" from="${Country.list()}" optionKey="id" />
.. is changed to ..
<g:select name="user.country.id" from="${Country.list()}" optionKey="id" />
You should also look into command objects. They can validate and bind all of your parameters at once.

Is This Unexpected Behavior for the CheckBox HtmlHelper?

My view page contains a search-by-example form with the following checkbox code:
<td>
<label for="HasProcessErrors">Has Errors:</label>
<%= Html.CheckBox("HasProcessErrors", crit.HasProcessErrors) %>
</td>
The crit object HasProcessErrors property is a boolean whose initial value is false. When I view the source of my rendered page, I see that the helper has generated the following HTML:
<td>
<label for="HasProcessErrors">Has Errors:</label>
<input id="HasProcessErrors" name="HasProcessErrors" type="checkbox" value="true" /><input name="HasProcessErrors" type="hidden" value="false" />
</td>
Have I used the CheckBox helper incorrectly here, or is something odd going on? It seems like it should generate an input of type checkbox with checked = "".
Thanks for any ideas.
Yes this is correct.
The semantics of a checkbox are a little bit different from what you may think; instead of posting a value indicating its checked/unchecked state, a checked checkbox posts whatever is in its ‘value’ attribute, and an unchecked checkbox posts nothing.
As there is also a hidden field with the same name, if you debug your form submit, you will find a checked checkbox has the value 'true,false' whilst an unchecked box has the value 'false'
You can determine if a checkbox is checked by testing if it contains 'true'.
public ActionResult(FormCollection form)
{
bool checked = form["checkbox_id"].ToString().Contains('true');
}

Resources