Unique key parts not showing in Create Form Grails - grails

I have following Domain Class -
class xyz {
String name
String version
String vclass
Date dateCreated
static constraints = {
version(blank:false)
name (unique: ['version'])
}
static hasMany = [ yz: Yz ]
}"
when i create a record for it, i don't see an option to enter version, only name and vclass. Why?

In Grails, version is a special property of Domain classes used for optimistic locking. Rename your property to something other than version. You can read more about this in the user guide.

Related

Grails scaffolding, still can't process java8 localdateTime in edit forms, nor can it do Map property

I am using Grails v3.3.9, latest grails hibernate 8 and grails plugins in build.gradle, along with java8 plugin, fields plugin (didn't make any difference) and Hibernate 8.
compile "org.hibernate:hibernate-core:5.1.5.Final"
compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.1.5.Final'
compile "org.grails.plugins:gsp"
compile 'org.grails.plugins:grails-java8:1.2.3' //java 8 features including datetime in GSP
compile 'org.grails.plugins:fields:2.2.10'
I've tried this on my Domain model which extends an abstract class with LocalDateTime
abstract class Agreement {
String contractReference
String status
LocalDateTime contractSignedDate
LocalDateTime lastUpdated //updated by framework
LocalDateTime dateCreated //updated by framework
static constraints = {
contractReference nullable:false
status nullable:false
contractSignedDate nullable:true
}
}
and concrete class here
class MaintenanceAgreement extends Agreement {
String level
Map category = [:] //p1 to p5 and sla details
//static belongsTo = [serviceProvider : OrgRoleInstance, maintainer: OrgRoleInstance]
// implemented as unidirectional many to one ! mag point to org
static belongsTo = [maintainer: OrgRoleInstance]
static constraints = {
level nullable:false
//serviceProvider nullable:true
maintainer nullable:false //ref to maintainer party
category nullable:false
}
}
If I have a record in Db from bootstrap with null column - grails just doesn't how any field holder for null entry. Go back to bootstrap and manually create a value, restart.
At least this time list and show rendering put an actual on the form
However when you go to create a new form (or the edit action) you get this. The contractSigned (localDateTime) has no editor, and lastly the domain model has a property category as a Map, and there is no editor for Map column.
This is a real problem. I can revert back to trying to use Date but that's a retrograde step really, and the platform suggests the new data time formats are supported. The Map column to think what my fallback for that might.
Am I missing something here that makes this work ? or do I have re raise as defect ?
I ran into this in previous 3.3.8 build here also with no resolution to data time issue
previous record
see also previous ask for feature here
make java new dates first class citizen in grails
Don't use LocalDateTime in you class for data type. You might not able to edit that. Use Date type instead. Then grails itself will provide you date-picker to add and edit as well. If you need to extract current date time in GSP you can use ${new Date() as value of that field.

Is any package name restrictions applied in grails?

I have created a Domain class called Action under package promotion
package promotion
class Action {
String name
String code
String address
static constraints = {
name blank:false
code maxSize:5
address nullable:false
}
}
Issue: Here my problem is I can not generate controller&views. And also I can not change default error messages from message property for my constraints
example: promotion.Action.name.nullable=Please enter the action name
Note: But if I change my package name "promotion" to something like "com.app.act" it works fine.
Try to make package name little brief. So it can be a good coding practice also

Grails application design

In Grails application i have EmploymentSeeker domain as below :
class EmploymentSeeker {
String fullName
String expYears
Boolean haveDrivingLic
String gender
String eduQualification
String courses
String currentTitle
int age
}
I want to make the user in the create view to add previous references. user may add up to 4 references , but I don't know how to do it - shall I create Emp references domain and link it to employment seeker domain static belongsTo?

Grails: How to assign domain field = someValue fetching from database

Suppose I have two domains:
class Country {
String name
String Desc
static hasMany = [organizations: Organization]
}
class Organization {
String name
Country country
static belongsTo = [country:Country]
}
Note: In case of create Organization, countries will be listed in a combobox.
Suppose I have 4 row in my database which I have fetched from bootstrap.
**Name** **Desc**
US USA
CA CAN
NP NEP
IN IND
I want to assign USA as a default value while create a Organization. Is there a way to do it from domain level not gsp?
I think the load method is your best bet, although it requires that you know the id of the instance that you want to be the default. It's not a good idea to hard-code this because it could change in the future. But you can look it up when the app starts and save the id, e.g.
class BootStrap {
def grailsApplication
def init = {
def usa = Country.findByName('US')
grailsApplication.config.COUNTRY_US_ID = usa.id
}
}
Use whatever Config key you want of course.
Then change the Organization class to call load in its constructor:
import grails.util.Holders
class Organization {
Organization() {
if (Holders.config.COUNTRY_US_ID) {
country = Country.load(Holders.config.COUNTRY_US_ID)
}
}
String name
static belongsTo = [country:Country]
}
Note that I removed the Country country property because it's redundant; adding that belongsTo creates a property with the same name as the map key ("country") and with the same type as the map value (Country). It doesn't hurt to have it, but it's not needed.
The reason load is appropriate here is that it doesn't hit the database until you access a persistent property other than id. So you're configuring it to lazily retrieve the real instance if necessary, but if you overwrite the value with a different instance, you won't have wasted a database lookup for the initial population. Turn on SQL logging to verify this.

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.

Resources