How can fields in Grails represented by a combobox be made optional? - grails

I'm doing my first experiments with Grails and am looking for a way to have fields represented by a combobox (such as one-to-one domain associations and numbers with a narrow range constraint) to be optional, i.e. there should be an empty entry in the combobox.
How can this be achieved? I've tried both adding a nullable:true constraint and listing the fields in the optionals static property, but neither produces the desired result.
These are my domain classes:
class Customer {
String name
}
class Book {
static optionals = ['year','loanedTo','loanedSince']
static constraints = {
title(blank:false)
author(blank:false)
year(range:1900..new Date().getAt(Calendar.YEAR), nullable:true)
loanedTo(nullable:true)
loanedSince(min:new Date())
}
String title;
String author;
Integer year;
Customer loanedTo;
Date loanedSince;
}

I've found that the nullable:true constraint actually does have the desired effect - however, it does not take effect immediately; you have to restart Grails to see it.

If you've generated your scaffolding code, you'll also have to regenerate it so that the option is present.

I don't think optionals is still supported: http://jira.codehaus.org/browse/GRAILS-472

The tag also has an attribute for a default, "not selected" value: noSelection. You can use it like this to have the drop-down default to "---" instead of your regular values:
noSelection="${['':'---']}"
In the controller, the default value shows up as an empty string, as specified in the first part of the value.

Related

Grails number field won't allow blank answer

I am using Grails 3. I have the following field:
<g:field id="myVar" name="myVar" type="number" value="${this.myController?.myVar}"/>
Domain class:
class myDomain{
int myVar
static constraints ={
myVar nullable:true, blank:true
}
}
When I try to submit this field, it will not allow a blank answer. I have it set up in my constraints in the domain class that this field can be null and blank. All of the number fields inside of my form are giving me this error, yet I can leave other fields blank.
This is the error message:
Property myVar is type-mismatched
Is there a setting I am missing?
int is a primitive type. It will never be null. You should have Integer. Also you should remove "blank" constraint. It make sense only for String type.

Trim domain field by default

What is the best way to trim field value in domain?
My suggestion is to use beforeSave(), but would work something like this?
class Book {
String name = name?.trim()
}
You have a couple options depending on what behavior you want.
A custom setter, which will trim the value every time you set it
class Book {
String name
void setName(String name) {
this.name = name?.trim()
}
}
A custom getter, which will give you a trimmed value but not store it in the database
class Book {
String name
String getName() {
this.#name?.trim()
}
}
A hibernate event, like beforeSave() as you mentioned, which will only trim it before the object is persisted.
Well, you can enable automatic trimming of string in Grails (version 2.3+) by setting below property in Config.groovy file:
grails.databinding.trimStrings = true
This will automatic trim the string before save or update.
I have have noticed that Grails automatically does a .trim() on fields before persisting them. For example:
null
""
" "
All gets stored as null in Grails 2.3.7 for a nullable string. In addition:
" foobar "
gets stored as "foobar"
These results are using the default h2 database. So you might let Grails do the heavy lifting in this case.
Here is my hack for quickly trimming all fields in a domain object. I often receive JSON data that is not formatted in a way that would allow me to use data-binding techniques. This method can be called after updating or assigning all values in the domain instance.
class Book {
def trimFields() {
this.properties = this.properties
}
}
Requires this configuration which is set by default in Grails
grails.databinding.trimStrings = true
I know this is overkill but it's quick and easy to add to a domain class.

Domain object string field auto trimming

After update from Grails 2.2.3 to Grails 2.3.5 (groovy 2.0.8->2.1.9) I found strange behavior
Domain object:
class Element {
String name
String title
static constraints = {
title nullable: true
}
}
During creation String field trims automatically and empty string replaced by null
def e = new Element(name:'', title:' sgg gg ')
assert e.name==null
assert e.title=='sgg gg'
I can't find this super feature in changelog of Grails & groovy. How I can disable it?
From: http://grails.1312388.n4.nabble.com/Grails-2-3-Data-Binding-String-Trimming-And-Null-Conversions-td4645255.html
The default behavior in Grails 2.3 is to trim strings during data binding. In addition to that, another default behavior is to convert empty strings (strings with nothing in them, not even spaces) to null during data binding. Those 2 things happen in that order so if you bind a String with nothing in it but spaces, the default behavior is to bind null because the String will be trimmed and then since it is empty it will be converted to null. This is a sensible default. There are separate config properties for disabling either of those behaviors.
// grails-app/conf/Config.groovy
grails.databinding.convertEmptyStringsToNull=false
grails.databinding.trimStrings=false
I believe it's mentioned here in the documentation

Property [] of class [] can not be null error in Grails

I am new to Grails and getting this error on two out of my three domains when I run the project on all the domains content. The domain the works is
class Location {
def scaffold = true
String company
String name
String address
static belongsTo=[company:Company]
static constraints = {
}
}
The domain that does not work is
class Report {
def scaffold = true
String title
String location
Date published
static belongsTo=[location:Location]
static constraints = {
}
}
I can not see the structural or syntax issue that is trowing the errors. I have been trying a variety of adds and subtracts and can not seem to find anything that address this error at a basic level. Again I have just started using Grails and Groovy
After doing the changes told by #araxn1d, you should also check the constraints. By default all properties are not nullable (that's why you're getting for example the error for the title property)
If you already have data in your database you have two options:
Update your database and set the correct values in each table or row
Set each property in the domain as nullable. For example
static constraints = {
title nullable:true
}
Are you creating a Report with no properties set? By default, Grails will check that all properties are not null. If you want to allow the user to leave a certain field undefined (null), then you have to explicitly tell Grails in the constraints map:
static constraints = {
propertyName nullable: true
}
Seems like error is in this line:
String location
location is String type, but should be Locationtype. The same as String company in Location domain should be Company company.
I think the problem is that you've changed the structure of the domain class after you generated the automatically created views/controller for the domain.
I fixed it by just deleting the offending domain along with the controller/views file and recreating them. I'm sure you could root through and find the offending code though.

constraints in grails

Hi I am having some trouble getting my constraints to work in my grails project. I am trying to just make sure that the field for Site_ID is not left blank but it still accepts an blank input. Also I am trying to set the order in which the fields appear but even that is not reflecting on the page when I try it out. Here is the code:
package translation
class J2_Translations {
String Site_ID
String I18NKey
static constraints = {
Site_ID(blank:false)
I18NKey()
}
}
and here is my code for the controller, I am not doing anything special I just want the constraints to work
package translation
class J2_TranslationsController {
def scaffold = J2_Translations
}
thanks,
Ameya
Grails is a convention-over-configuration framework. Make sure you follow the standard Java naming conventions. Properties should be named with camel-case identifiers with the leading character in lowercase. For example:
String siteId
String i18nKey

Resources