验证的Grails不适用于非持久性域类 [英] Grails validateable not work for non-persistent domain class
问题描述
我遵循这里的指示: http://www.grails .org / doc / latest / guide / 7.%20Validation.html
并添加到 config.groovy 中:
grails.validateable.classes = [liningtest.Warm']
然后添加到 src / groovy / Warm.groovy 中(它是一个非持久性域类):
package liningtest
import org.codehaus.groovy.grails.validation.Validateable
class Warm {
字符串名称;
int happyCite;
Warm(String n,int h){
this.name = n;
this.happyCite = h;
static constraints = {
name(size:1..50)
happyCite(min:100)
}
}
但它不起作用(blank false&size:0 .. 25)为hasErrors函数。它总是返回false,即使名称> 25。
这是一个Grails错误,如果有,是否有解决方法?
我正在使用Grails 1.3.3
更新:我更新了简化代码。现在我知道约束size不能用于空白,但仍然无效。
我的测试类位于 test / unit /liningtest/WarmTests.groovy
package liningtest
导入grails.test。*
class WarmTests extends GrailsUnitTestCase {
protected void setUp(){
super.setUp()
}
保护void tearDown() {
super.tearDown()
}
void testSomething(){
def w = new Warm('Hihi',3)
assert( w.happyCite == 3)
assert(w.hasErrors()== true)
}
}
以及我得到的错误:
<?xml version =1.0encoding =UTF-8?>
testteite errors =1failures =0hostname =evolus -50b0002cname =liningtest.WarmTeststests =1time =0.062timestamp =2010-12-16T04: 7\" 点47分>
< properties />
< testcase classname =liningtest.WarmTestsname =testSomethingtime =0.062>
< error message =方法的签名:liningtest.Warm.hasErrors()适用于参数类型:()values:[]
可能的解决方案:hashCode()type =groovy。 lang.MissingMethodException> groovy.lang.MissingMethodException:方法没有签名:liningtest.Warm.hasErrors()适用于参数类型:()values:[]
可能的解决方案:hashCode()
在liningtest.WarmTests.testSomething(WarmTests.groovy:18)
< / error>
< / testcase>
< system-out><![CDATA [ - testSomething--
的输出]]>< / system-out>
< system-err><![CDATA [ - testSomething--
的输出]]>< / system-err>
< / testsuite>
更新2 :当我不使用单元测试时,在控制器中调用 hasErrors ,它将运行但返回false值。 (hasErrors用Warm返回false('Hihi',3))。有没有人有线索?
更新3 :我遵循Victor方式,现在如果我在hasErrors之前调用validate ()。但是我仍然不明白,为什么grails generate-all控制器在使用hasErrors()之前不必调用validate()?解决方案 / div>
您真的之前需要调用 validate()
- 它会触发验证并更改对象状态。看看 ValidationGrailsPlugin.addValidationMethods()
,我看到hasErrors()是一个只读方法。
示例在调用validate()后为我工作。我尝试了 grails console
(很棒的工具,我强烈推荐它!):
Warm w = new Warm('')
w.hasErrors()//'结果:false'
w.validate()
w.hasErrors()//'结果:true'
我将 @Validateable
添加到温暖的班级。我相信这没什么区别。
I followed the instruction here: http://www.grails.org/doc/latest/guide/7.%20Validation.html
and added into config.groovy:
grails.validateable.classes = [liningtest.Warm']
Then added in src/groovy/Warm.groovy (it's a non-persistent domain class):
package liningtest
import org.codehaus.groovy.grails.validation.Validateable
class Warm {
String name;
int happyCite;
Warm(String n, int h) {
this.name = n;
this.happyCite = h;
}
static constraints = {
name(size: 1..50)
happyCite(min: 100)
}
}
But it just doesn't work (both "blank false" & "size: 0..25") for the "hasErrors" function. It always returns false, even when the name is > 25.
Is this a Grails bug, if yes, is there any work-around?
I'm using Grails 1.3.3
UPDATE: I have updated the simplified code. And now I know that constraint "size" can't be used with "blank", but still does not work.
My test class in test/unit/liningtest/WarmTests.groovy
package liningtest
import grails.test.*
class WarmTests extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testSomething() {
def w = new Warm('Hihi', 3)
assert (w.happyCite == 3)
assert (w.hasErrors() == true)
}
}
And the error I got:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="1" failures="0" hostname="evolus-50b0002c" name="liningtest.WarmTests" tests="1" time="0.062" timestamp="2010-12-16T04:07:47">
<properties />
<testcase classname="liningtest.WarmTests" name="testSomething" time="0.062">
<error message="No signature of method: liningtest.Warm.hasErrors() is applicable for argument types: () values: []
Possible solutions: hashCode()" type="groovy.lang.MissingMethodException">groovy.lang.MissingMethodException: No signature of method: liningtest.Warm.hasErrors() is applicable for argument types: () values: []
Possible solutions: hashCode()
at liningtest.WarmTests.testSomething(WarmTests.groovy:18)
</error>
</testcase>
<system-out><![CDATA[--Output from testSomething--
]]></system-out>
<system-err><![CDATA[--Output from testSomething--
]]></system-err>
</testsuite>
UPDATE 2: When I don't use Unit test, but try to call hasErrors in the controller, it runs but return false value. (hasErrors return false with Warm('Hihi', 3) ). Does anyone has a clue?
UPDATE 3: I followed Victor way, and now the problem is solved if I call validate() before hasErrors(). But I still don't understand, why "grails generate-all" controllers doesn't have to call validate() before using hasErrors()?
You really need to call validate()
before - it will trigger validation and change object state. Looking at ValidationGrailsPlugin.addValidationMethods()
, I see that hasErrors() is a read-only method.
Your sample worked for me after calling validate(). I tried in grails console
(great tool, I highly recommend it!):
Warm w = new Warm('')
w.hasErrors() // 'Result: false'
w.validate()
w.hasErrors() // 'Result: true'
I added @Validateable
to Warm class. I believe it makes no difference.
这篇关于验证的Grails不适用于非持久性域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!