I have the following combobox:
<g:select name="ticketType" from="${app.domain.enums.TicketType?.values()}"
keys="${app.domain.enums.TicketType.values() }"
value="${ticketInstance?.ticketType}"
noSelection="${['null': 'Select One...']}"
/>
I've setup the following constraint for ticketType in command object
ticketType nullable: true, blank:true
TicketType is a very simple enum:
public enum TicketType {
QUESTION, SUPPORT, MAINTENANCE, NEW_FUNCTIONALITY, MALFUNCTION
}
And every time I don't setup some value for ticketType in my GSP I get the following error:
Failed to convert property value of type 'java.lang.String' to required type 'com.coming.enums.TicketPriority'
It's like in case of no selection g:select sets the value for "null" (string).
What am I missing?
Rather than using the 'null' literal, have you tried using an empty string as your noSelection attribute? e.g. noSelection="${['':'Select One...']}"? This may do a proper conversion to a true null value during data binding.
As your error says - you do have a string in your noSelection. This can't be converted to any of your enum values.
Remove the quotation marks of your null and it should work (it works for me with grails 2.0):
<g:select name="ticketType" from="${app.domain.enums.TicketType?.values()}"
keys="${app.domain.enums.TicketType.values() }"
value="${ticketInstance?.ticketType}"
noSelection="${[null: 'Select One...']}"/>
Under Rails 3.3.x, no variation of providing the "null" value worked. In the end, I resolved it by adding this line in to the controller+action before doing any use of the params:
// convert "null" strings to REAL nulls
params.account.each { it.value = (it.value == 'null' ? null : it.value) }
After that, the following worked fine:
account.properties = params.account
Related
Setup: Grails 2.5.6 with Hibernate 4.3.10
I have a table with a string id. Thing is, its values are numeric strings, and this seems to mess up get() when I pass in a value such as "000000".
Domain class:
class Term
{
static mapping = {
id name: 'code', generator: 'assigned'
version false
code column: 'CODE'
description column: 'DESC'
}
String code
String description
}
Data looks like:
CODE || DESC
-------++---------------------------
000000 || The Beginning of Time
201715 || Post Secondary Winter 2017
201815 || Post Secondary Winter 2018
999999 || The End of Time
And then in testing I found the following:
assert Term.list() // works fine
assert !Term.get('foo') // works fine
//assert Term.get('000000') // throws exception
The exception thrown is:
Method threw 'org.springframework.orm.hibernate4.HibernateSystemException' exception.
Provided id of the wrong type for class Term. Expected: class java.lang.String, got class java.lang.Long
org.hibernate.TypeMismatchException: Provided id of the wrong type for class Term. Expected: class java.lang.String, got class java.lang.Long
So it looks like at some point the '000000' and the '201715' and whatever else are being inconveniently converted into Long objects. Using as String doesn't help either. Can anyone help me tell Hibernate that this String should be treated as a String?
This seems like a Grails bug and I'm guessing it is because you have not declared id to be of type String in your domain class because it is mapped to a different field (which makes sense).
You could try adding
String id
to your domain class although that may not caused desired behaviour with column generation.
I would suggest rather than using get() you could use findByCode() as you have mapped your id to the code field and the result should be the same.
We are using Grails 2.4.3 , In our Command
#Validateable
class FreeTextSearchCommand {
String freeText="*"
int pageSize=1
int pageIndex=1
}
And in Controller
def freeTextSearch(FreeTextSearchCommand freeTextSearchCommand){}
So if value comes from Client site is null then I want my default value set , but it does not work
So if value comes from Client site is null then I want my default
value set , but it does not work
If you bind null to a property then the result should be null. That is what the data binder does and it is what the binder is supposed to do. If you want to impose some custom rules you can do that a number of ways. One is by using the BindUsing annotation.
#Validateable
class FreeTextSearchCommand {
#BindUsing({obj, source ->
// bind '*' if there is no corresponding request parameter value...
source['freetext'] ?: '*'
})
String freeText
// etc...
int pageSize=1
int pageIndex=1
}
See http://grails.github.io/grails-doc/2.4.3/guide/theWebLayer.html#dataBinding for more details.
I hope that helps.
I wrote a function in my service class where I evaluate a passed parameters from controller but Grails is returning wrong evaluation results.
def list(String q,String qval,String srt,String ord){
log.debug("q==="+q)
log.debug("qval==="+qval)
log.debug("srt==="+srt)
log.debug("order==="+ord)
all these debug statements print null as expected. Now
boolean qvalbool=qval?.trim()
log.debug("qvalbool===>>"+qvalbool) prints true!!!
!StringUtils.isEmpty(q) && !StringUtils.isEmpty(qval) returns true!!
both statements should return false while they are returning true what is going on with this? any ideas?
I'm using grails 2.4.2
Evan Wong's comment is very likely correct that you're seeing a string containing the word null.
Often when Groovy prints out values it's not apparent what their type is.
groovy:000> null
===> null
groovy:000> 'null'
===> null
Also in Groovy the expression null + '' evaluates to the String 'null'.
That would be an easy way to change the value of this parameter so it contains the string 'null'.
I have a domain class like this :
class City {
String name;
Country country;
}
In the create view I want to put country as a select box like this :
<g:select id="country" optionKey="id" optionValue="countryName"
from="${Country.list()}" name="country" >
</g:select>
What is the best way to handle the submit, so that the country is sent as an object
(currently i receive a message :
Failed to convert property value of type java.lang.String to required type com.example.Country for property country;
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.example.Country] for property country:
no matching editors or conversion strategy found
)
note : i found this matching question : Grails select not returning the object but a string but the solution didn't solve my problem.
Thank you.
This is because Grails controller doesn't understand what to do with numeric value of country request parameter. Thus, in your controller you should write:
def country = Country.get(params.getLong('country'))
def city = new City(name: params.name, country: country)
I have a nullable DateTime I want to show in ShortDate format if it has a value, but I can't get it right. I am trying to use the null operator ($!{}) here.
It should work like this:
<td>$!{period.Enddate.Value.ToShortDateString()}</td>
But this gives an InvalidOperationException: nullable object must have a value.
Removing the 'Value' part won't work either, that will give the obvious 'System.Nullable has no definition for ToShortDateString' message.
With the conditional operator it works fine, but that one only works for attributes like this:
<td value="$!{period.Enddate.Value.ToShortDateString()}?{period.Enddate.HasValue}"></td>
And I am trying to get it inside the td element, not as an attribute for td.
Am I doing something wrong here, or is this a known issue?
I understand that catching an InvalidOperationException (thrown by the Nullable class) is trickier than catching a NullReferenceException, but I think it is a serious flaw.
Cheers,
Ronald
As of Spark v1.6, here are some options:
use a format specfier -
<td>${ string.Format("{0:M/dd/yy}", period.Enddate) }</td>
or create an additional presentation property -
public string EnddateText
{
get
{
var result = Enddate.HasValue ? Enddate.Value.ToShortDateString() : string.Empty;
return result;
}
}
<td>${ period.EnddateText }</td>