How to set the constraints of dynamic scaffolding project? - grails

I'm very new to grails web application! Here is my domain class
package imocha.project
class Feedback {
String name
String email
String type
String remark
static constraints = {
name(blank:true)
email(blank:true)
type(blank:false)
remark(blank:false)
}
}
In my case, I just want to set only the type and remark can't be blank! But at the end, it set all for me.

The reason for this is the default behavior for properties is nullable: false in a Grails project. Alter your constraints as follows:
package imocha.project
class Feedback {
String name
String email
String type
String remark
static constraints = {
name(nullable:true, blank:true)
email(nullable:true, blank:true)
type(blank:false)
remark(blank:false)
}
}

Related

Creating Domain Relationships in Grails returns unable to resolve class error

Good day to all. I am very new in using grails and I have followed several tutorials for beginners using grails until I come up in creating domain relationships. However, I got stuck with this problem right now. I have 3 domain classes namely, todo, category and user. And as I defined their relationships, it returns me an error saying unable to resolve class. Please see my codes below. Please help. Thank you so much.
Todo.groovy Class
package todoScaff
class Todo {
String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category
static belongsTo = [User, Category]
static constraints = {
name(blank:false)
createDate()
priority()
status()
note(maxsize:1000, nullable:true)
completedDate(nullable:true)
dueDate(nullable:true)
}
String toString() {
name
}
}
Category.groovy Class
package categoryScaff
class Category {
String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]
static constraints = {
name(blank:false)
}
String toString(){
name
}
}
User.groovy Class
package userScaff
class User {
String userName
String fname
String lname
static hasMany = [todos: Todo, categories: Category]
static constraints = {
userName(blank:false, unique:true)
fname(blank:false)
lname(blank:false)
}
String toString(){
"$lname, $fname"
}
}
Since you've placed your domain classes in different packages, you must import the classes at the head of the file.
package categoryScaff
import todoScaff.Todo
import userScaff.User
class Category {
The same needs to happen in your other domain classes that reference classes outside the current package.

How do you organize the fields in grails 2.4.4 including the static mapping relations?

Okay, so I understand how to organize the fields in grails without using the gsp pages by writing the fields in the constraints like so.
Class User{
String firstName;
String nickName;
static constraints = {}
}
this will make first name appear before nick name in the default scaffolding because f comes before n in the alphabet.
Class User{
String firstName;
String nickName;
static constraints = {
nickName()
firstName()
}
}
this makes nick name appear before first name in the CRUD model in scaffolding. It's the order you name the constraints.
Now, how do you make the relations appear in a specific order? For example if I had this
Class User{
String firstName;
String nickName;
static belongsTo = {company:Company}
static constraints = {
}
}
How would I rearrange this order? would it be done in constraints? I know it can be done in gsp page, but how would I do it here?
I'm pretty sure it's the same as regular fields...
i.e. if you want the order of the fields to appear as nickName, firstName, company you'd do this:
Class User {
String firstName
String nickName
static belongsTo = {company: Company}
static constraints = {
nickName()
firstName()
company()
}
}

How can I change the default Grails/GORM lookup?

I'm new to Grails. I'm developing a web app that handles the records of a gymnasium, to make routines, exercises, etc.
I have this domain class Ejercicios:
class Ejercicios {
String nombreEjercicio
String idHoja
String descripcion
List<String> descripcionO
static hasMany = [descripcionO: Descripciones]
static transients = ['descripcionTrans']
String descripcionTrans
static mapping = {
id column: "idHoja"
version false
}
static constraints = {
nombreEjercicio maxSize: 45
idHoja blank: false
}
The database table has the default Grails id named "idHoja", and another attribute named "id_hoja"
The thing here is that when I make a JSON parse from the rest API, I need GORM to look for exercises via the "id_hoja" attribute, not the "idHoja" because it'll cause a mismatch.
I found the solution by myself!
The only thing I needed to was to make the JSON call with the name "idHoja" and that was it.

Grails domain class unit test errors

I wrote the following domain class with its unit test. When I run the tests, I get the error message
No such property: admittedMobileUser. When I comment that line in domain class I get the same problem on other fields. Does anyone knows how to solve it?
class MobileUser {
String userName
String userDevice="Android"
Integer userCluster
Boolean admittedMobileUser
Date lastTimeUpdatedUserSpaceTime=new Date()
byte[] userSpaceTimeXml
static constraints = {
userName blank:false, unique:true
userDevice blank:false
userCluster validator : {val-> return val > 0}
admittedMobileUser
lastTimeUpdatedUserSpaceTime
userSpaceTimeXml maxSize:1024*1024
}
String toString(){
return "${userName}_${userCluster}"
}
}
remove such rows from constraints :
....
admittedMobileUser
lastTimeUpdatedUserSpaceTime
....
or add minimum one constraint to each!

Grails using hibernate basic types

I have a small grails service that I created, and I am trying to use the type:'text' on a member of a model. I always see this field come up as a varchar(255) however, even though I have dropped the database and had it recreated.
I essentially have:
class eventParameter{
static belongsTo = [logEvent:LogEvent]
String name
String value
static constraints = {
name blank:false
value blank:false
}
static mapping = {
value type:'text'
}
}
Does anyone have any idea why this does not create the right type of column?
To override the underlying database type, use the sqlType mapping. For example:
static mapping = {
value sqlType:'text'
}
Also, check out the relevant section of the Grails Manual: 5.5.2.10 Custom Hibernate Types

Resources