I have a text field whose length I would like to limit at the maxSize constraint of one of my domain classes.
So if I have a class foo:
class Foo {
String bar
static constraints = {
bar(maxSize: 100)
}
}
I would like to get that value of 100 for the property bar. Is this possible?
You should be able to do:
def maxBarSize = Foo.constraints.bar.getAppliedConstraint( 'maxSize' ).maxSize
I was having this issue in grails 3.1.8 and it has change a bit. at least in the gsp views I had to put this:
Foo.constrainedProperties ['bar']['maxSize']
Hope this help! Cheers!
Check the following code:
def foo = new Foo(bar: "stuff")
println foo.constraints.bar.maxSize
Related
Can anyone please help me.I have two domains.
Class Parent{
static hasMany = [child: Child];
}
Class Child{
}
In database there are 20 records.I want to get 10 records only without refreshing the page.So i used remote pagination like.
gsp code is
<util:remotePaginate controller="Parent" action="show"
total="${parentList.size()}"
update="updatedListId" max="10" pageSizes="[10, 20, 50,100]"/>
In Controller i wrote like.
def parent =Parent.get(1);
def parentList = parent.getChild();
I tried this one but its is not working.
def childs = Child.findAllByParent(parent, [max: 10])
It is giving all records but i need to here get only 10 records. I set params max value and pass it as argument but its not working.
please help me.
thanks
You can get the childs in a join via HQL:
Child.executeQuery("select c from Parent p join p.childs c where p=:p", [p:p, max:10])
Have you tried where query?
// Parent.groovy
class Parent{
static hasMany = [ children: Child ]
}
// Where query in controller/service
Parent.where { id == 1L }.children.list(max: 10)
at the moment I try to deal with scaffolding in grails. There I have a little problem with the generation-templates where i don't find a solution. I want to configure the generating-algorithm in the domainmodel.
My thought was to define static variables in the model which aren't created in the database and only important for the generation-process.
For example I want to display only some special fields in the show.gsp but i want to display every field in the _form.gsp Or i want to do some gsp-imports but only in seperate gsp.
So i thought that i could define some static variables where the value contains some configuration-parameter i can interpretate in the generation template.
I hope everybody understand what I mean ?
Here is an example:
class Awesome {
Long awesome_level
Long person_name
Boolean itsMe
static String showFields
static transients = ['showFields']
static constraints = {
einrichtungs_type()
type_of_conzept()
anzahl_gruppen()
anzahl_kinder_pro_Gruppe()
offnungszeiten()
showFields(["person_name", "itsMe"])
}
In the Show-View i only want to display the fields in the array "showFields"
...
for (p in props) {
if (p.embedded && p.name in domainClass.showFields) {
def embeddedPropNames = p.component.persistentProperties*.name
def embeddedProps = p.component.properties.findAll { embeddedPropNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(embeddedProps, comparator.constructors[0].newInstance([p.component] as Object[]))
%><fieldset class="embedded"><legend><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></legend><%
for (ep in p.component.properties) {
renderFieldForProperty(ep, p.component, "${p.name}.")
}
%></fieldset><%
} else {
renderFieldForProperty(p, domainClass)
}
...
I know that the if clause don't work. My problem is, that i am not able to get the value of the field "showFields".
Know my questions:
Is it able to receive the values of a field of a domainclass?
Is it able to execute a methode of a domainclass?
Is there an other way do define configuration-parameters which i can access in the generation-templates?
I hope i was able to display my problem and thank you for some help!
Greetz
V
I found a solution for that problem. First of all I thought it could be possible with the creation of a custom-Constraint. I am still thinking that this is also possible but i found a better /easyer way to add "configurations".
I use the tag attributes, which already exsits. If i understand it right, the attributes parameter i used to add attributes in select-Html-tags. Now i use it for adding some configuration-parameters. Here my solution:
You have to change the "renderEditor.template" that not all the configuration-parameter will add in a HTMl-tag as a attribute. cp.attributes.each { k, v ->
sb << "${k}=\"${v}\" "
} change to cp.attributes?.realAttributes.each { k, v ->
sb << "${k}=\"${v}\" "
}
When you realy want to this attributes-function add into the attributes-parameter the value "realAttributes" (or how you would like to call it)
Change the constraints of the model: (in my example:)
static constraints = { einrichtungs_type(attributes:
[showField:true])
}
Finaly change the generation-template
if(cp.attributes?.showField){
...
I hope these 4 steps help you, if you have nearly the same problems.
Greetz
V
The next code works:
self query1 = DomainClassExample.findAllByPropertyInList("hi","bye")
But if I add Not, the dynamic finder does not exist (it DOES exists: check the answer):
self query2 = DomainClassExample.findAllByPropertyNotInList("hi","bye")
I want to get all the DomainClassExample which don't have the property "hi" or "bye". Is there any dynamic finder for that purpose? Should I use the Where Query. How?
First, a minor correction. It's not a method expression. It's a dynamic finder. Now, as to your actual problem, you'll need to use the criteria API for this...
def c = DomainClassExample.createCriteria()
def matchingProperties = c.list {
property {
not { 'in'(["hi","bye"]) }
}
}
You might run into an issue with the word 'property' and I haven't actually tested the code I just wrote. But that is the gist of it.
In Grails 1.3.9, given the following domain:
class User {
String username
....
}
this works in our application without any issues:
def userList = User.findAllByUsernameNotInList(["user1#testdomain.com","user2#anotherdomain.com"])
I have the folowing:
class Store{
String name
}
class Shop{
String name
Store store
}
My criteria builder:
def c = Shop.createCriteria()
def results = c.list {
like("name", "Harrods")
like("store.name", "McDonals")
}
I'm sure this is invalid cause i'v tested it. How can i manage to use criteriaBuilder and do this: like("store.name", "McDonals")?
Looking forward to get any help,
John
Since you're querying an association, try:
def results = c.list {
like('name', 'Harrods')
store {
like('name', 'McDonals')
}
}
This will do an conjoined query between name and store.name.
Check out the documentation Looks like you need to use a % for your like clause.
i have domain classes:
package test
class Credit {
String name;
static hasMany = [debts : Debt]
static constraints = {
}
}
and
package test
class Debt {
Integer amount;
Date date;
static belongsTo =[credits: Credit]
static constraints = {
}
}
Need: select max: 10; order: "desc"; sort: "date" rows of Debt associated with the Сredit.get(id)
How can i do it?
solution:
Debt.findAllByCredits(Credit.get(params.id),[max:10, sort:"date",order:"desc"])
but next question about this example:
why, this code work:
def ok = Debt.findAllByCredits(Credit.get(params.id),[max:10, sort:"date",order:"desc"])
println "true:" + ok
but this code not work correct:
def dd = new Debt(credits: Credit.get(params.id))
def wrong =Debt.findAll(dd)
println "no: "+ wrong
all time return all records in table, why?
you could do something like
def hql = "select d from Debt d where credits = ? order by d.date desc"
Debt.findAll(hql, [credit], [max:10])
you might have to tweak that, but something similar should work. Also note, I am assuming that you have an instance of credit that is the parent of the Debts.
You can also use the methods Grails generates dynamically at runtime, based on the properties of your classes
Debt.findAllByCredit(credit, [max:10,sort:"date",order:"desc"]
again, you will need a reference to a credit object.
Check out the docs at
http://grails.org/doc/latest/
specifically the section on findAll and findAllBy under Domain Classes in the left hand nav.