constraints in grails - 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

Related

How to make ID column visible in the list view

I have the following Groovy Domain class and generated scaffold controller for it.
When I create a Book row in the table, the ID column is invisible on the list view.
Is there anyway to make ID column visible on the view? I tried visible:true but it seems not making any difference.
class Book {
String bookAuthor
static constraints = {
bookAuthor blank: false, maxSize:30
}
static mapping = {
version false
id generator: 'sequence',
params: [sequence:'s_book_seq']
}
}
Edit the generated scaffolding views and add an id field manually. If you need to do this for a large number of domain classes, modify the scaffolding templates instead; you can install the templates using grails install-templates, they will be copied into src/templates

Custom Grails Constraint doesn't seem to work

I have been trying to create a custom constraint in a Grails Project (see constraint code below).
import org.codehaus.groovy.grails.validation.AbstractConstraint
import org.springframework.validation.Errors
class BuscaConstraint extends AbstractConstraint {
public static final String CONSTRAINT_NAME = "busca"
protected void processValidate(Object target, Object propertyValue, Errors errors) {
}
boolean supports(Class type) {
return type && String.class.isAssignableFrom(type);
}
String getName() {
return CONSTRAINT_NAME;
}
}
As you can see, this constraint doesn't actually validates anything. Instead, it's just a flag to customize the property's rendering in the scaffolding generation.
After creating the class above, I added the following line in the file Config.groovy:
ConstrainedProperty.registerNewConstraint(BuscaConstraint.CONSTRAINT_NAME, BuscaConstraint.class)
..and added this constraint to a class's property:
class ThatClass {
def someProperty
static constraints = { someProperty (unique:true, busca: "nome")
}
However, if I try to get the result of the expression
ThatClass.constraints.someVariable.getAppliedConstraint("busca"),
all I get is null.
I based my approach in some blog posts such as this one and from a constraint in Grails' github repo(however I can't see how they are configured there).
What am I doing wrong?
Has the configuration of Grails' custom constraints changed recently?
It looks your constraint is good. I use something very similar in my Url Without Scheme Validator Plugin, in class UrlWithoutSchemeConstraint and it works like a charm in recent Grails (2.3.x, 2.4.x).
However, I never tried to access it at runtime, so I'd try to investigate this area. For example, have you tried ThatClass.constraints.someProperty.busca?
I am also using my constraint as a tag in a Grails 2.4.4 project. The constraint can be accessed with the following code:
domainClass.constraints[p.name].getMetaConstraintValue("encodeAsRaw")
Where p.name is the property name and "encodeAsRaw" is the name of my constraint. I am using this code successfully in a couple of .gsp files.
If just using as a tag, you don't even need to create a custom constraint class and register it. It's enough just to check for its existence with the getMetaConstraintValue method.
For completeness, here is my constraint definition in my domain class:
myProperty(nullable:true, blank:true, unique:false, maxSize:1000, encodeAsRaw:true)

Grails Scaffolding

I am recently working on grails and would like to know how to do more complex scaffolding
For example, if I want to Scaffold a class
class Book{
Author a
Publisher p
// ....
}
Author class
class Author{
String firstName
String lastName
// ...
}
Publisher class
class Publisher{
String name
String address
// ....
}
Now if I have a BookController
class BookController{
static scaffold = true;
}
I have a layout of
Author Publisher
However if I want a layout with
AuthorID AuthorFirstName AuthorLastName PublisherName PublisherAddress
I have looked through the http://grails.org/doc/latest/guide/scaffolding.html, however, I am unable to set it to the given property. I would like to know I am able to accomplish it? A tutorial would be helpful.
The scaffolding plugin within Grails is not designed to handle these types of complex views out of the box. You have a few options:
Use the install-templates command and modify the scaffolding templates to handle your needs.
Re-design your domain class to use embedded Author and Publisher. This will change the scaffolding output, but it also will change a lot more too. I wouldn't use this option unless you understand all the changes this will make to your domain model.
Generate the code using scaffolding then customize the output to suit your needs.
Of the three options presented here I would recommend the third as it makes the most sense to address the narrow scope of your issue.
You could also use transients. But transients aren't displayed by default.
You need to modify the templates and explicitly hide otherwise hidden fields using constraints i.e. id
NOTE: code below untested, for illustration purposes only.
//optional, but allows code completion in IDE ;-P
String authorName
String getAuthorName(){
return a.firstName + ' ' + a.lastName
}
static transients = [authorName:String]
static constraints = {
id(display:false)
}

Find type of groovy/grails property

I am editing the scaffolding templates for a grails application.
One of my domain classes has the following property:
Set<GlobalRole> globalRoles
where GlobalRole is an enum. The link is one-to-many:
static hasMany = [globalRoles: GlobalRole]
As default scaffolding shows the roles as a comma separated string - my aim is to show it as a list. To this aim I need to find out if a property (globalRoles) is of type Set to differentiate the generation of the scaffolding.
if (User.globalRoles.type instanceof Set){
// do something else
}
However: this statement is 'falsified' and therefore not working.
Am I missing something here?
you are trying to get the static User.globalRoles field which is wrong of course. You shall be using reflection / meta-programming:
if( User.hasMetaProperty( 'globalRoles' ) && Set.inAssignableFrom( User.getMetaProperty( 'globalRoles' ).type ){

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