Grails rejectValue-导致ob.errors null的多个检查
“我的域对象预订”有多个允许为空的属性,因为这些属性将在对象保存到数据库之后设置 myService.action()的一部分: 我正在使用 hasErrors(bean:booking,字段:'contactFirstname','has error')) 标记错误字段 如果我现在提交的表单中文本字段中没有任何值,则所有字段都是红色,booking.errors的错误大于0 如果我在提交表单后使用名字,booking.errors为NULL,并且没有标记其他字段 这是虫子吗?我支持Grails2.3.6 其他信息Grails rejectValue-导致ob.errors null的多个检查,grails,Grails,“我的域对象预订”有多个允许为空的属性,因为这些属性将在对象保存到数据库之后设置 myService.action()的一部分: 我正在使用 hasErrors(bean:booking,字段:'contactFirstname','has error')) 标记错误字段 如果我现在提交的表单中文本字段中没有任何值,则所有字段都是红色,booking.errors的错误大于0 如果我在提交表单后使用名字,booking.errors为NULL,并且没有标记其他字段 这是虫子吗?我支持Grails
现在我已经完全了解了情况,而且因为我睡眠困难,我想我会给你一个非常简洁的答案,这样你就有希望完全理解并正确地使用东西 首先,我知道创建一个验证bean听起来需要做很多工作,所以让我来教你如何相对简单地完成这一切,以及为什么它是我首选的方法 这是我喜欢的方法,因为当你这样做的时候 类MyController{
def myAction(Mybean bean) {
// 1. the object allowed into this save action
// are all that is available objects withing MyBean.
// If it has user define but not telephone. Then
// if telephone is passed to myAction it will fail and not recognise
// field
// When declaring Date someField or User user then the params now
// received as bean this way is now actually properly bound
// to the data / domainType declared.
// Meaning user will now be actual user or someField actually Date
}
现在来解释如何最好地解决这个问题。创建bean时,只需将实际域类从域文件夹复制到Grails2中的src/groovy/same/package
或Grails3中的src/main/groovy/same/package
将名称/类别或副本从Booking
更改为BookingBean
,使其具有不同的名称
在Grails2中将@validatable
添加到实际BookingBean
之上,或者将实现添加到主类中,如Grails3中的类BookingBean实现可验证的
现在,由于它被复制,所有对象都是相同的,此时,从控制器进行的保存将是相同的
class MyController {
def myAction(BookingBean bean) {
Booking booking = new Booking()
// this will save all properties
booking.properties = bean
booking.save()
}
}
但是您有一个特殊的情况,您想在主域类中声明一个临时字段,而我要做的是
class BookingBean {
def id
String contactFirstname
String contactLastname
boolean secondSave=false
static constraints = {
id(nullable: true, bindable: true)
contactFirstname(nullable:true) //,validator:checkHasValue)
contactLastname(nullable:true) //,validator:checkHasValue)
secondSave(nullable:true,validator:checkHasValue))
}
//use the same validator since it is doing identical check
static checkHasValue={value,obj,errors->
// So if secondSave has a value but contactFirstName
// is null then complain about contactFirstName
// you can see how secondSave gets initialise below
//typical set this to true when you are about to save on 2nd attempt
//then when set run validate() which will hit this block below
// Check all the things you think should have a
// value and reject each field that don't
if (val) {
if ( !obj.contactFirstname) {
errors.rejectValue('contactFirstname',"invalid.contactFirstname")
}
if ( !obj.contactSecondname) {
errors.rejectValue('contactSecondname',"invalid.contactSecondname")
}
//and so on
}
}
现在在控制器中:
class MyController {
def save1(BookingBean bean) {
Booking booking = new Booking()
// this will save all properties
booking.whatEver = bean.whatEver
booking.save()
// you can choose to validate or not here
// since at this point the secondSave has
// not been set therefore validation not called as yet in the bean
}
//您可能有id,它应该与实际的域类绑定
def save2(BookingBean bean) {
booking.secondSave=true
if (!bean.validate()) {
//this is your errors
//bean.errors.allErrors
return
}
//otherwise out of that loop since it hasn't returned
//manually set each object
booking.contactFirstname=bean.contactFirstName
booking.contactSecondname=bean.contactSecondname
booking.save()
}
}
e2a旁注-以上应回答
在创建之前不要验证它。只有在创建了对象并添加了值之后才进行验证。或者在验证bean中创建一个函数,作为第二次检查的一部分运行。未验证被称为,现在我完全理解了情况,因为我睡眠有问题,我想我给你一个非常简洁的答案,这样你就有希望充分理解并正确使用 首先,我知道创建一个验证bean听起来需要做很多工作,所以让我来教你如何相对简单地完成这一切,以及为什么它是我首选的方法 这是我喜欢的方法,因为当你这样做的时候 类MyController{
def myAction(Mybean bean) {
// 1. the object allowed into this save action
// are all that is available objects withing MyBean.
// If it has user define but not telephone. Then
// if telephone is passed to myAction it will fail and not recognise
// field
// When declaring Date someField or User user then the params now
// received as bean this way is now actually properly bound
// to the data / domainType declared.
// Meaning user will now be actual user or someField actually Date
}
现在来解释如何最好地解决这个问题。创建bean时,只需将实际域类从域文件夹复制到Grails2中的src/groovy/same/package
或Grails3中的src/main/groovy/same/package
将名称/类别或副本从Booking
更改为BookingBean
,使其具有不同的名称
在Grails2中将@validatable
添加到实际BookingBean
之上,或者将实现添加到主类中,如Grails3中的类BookingBean实现可验证的
现在,由于它被复制,所有对象都是相同的,此时,从控制器进行的保存将是相同的
class MyController {
def myAction(BookingBean bean) {
Booking booking = new Booking()
// this will save all properties
booking.properties = bean
booking.save()
}
}
但是您有一个特殊的情况,您想在主域类中声明一个临时字段,而我要做的是
class BookingBean {
def id
String contactFirstname
String contactLastname
boolean secondSave=false
static constraints = {
id(nullable: true, bindable: true)
contactFirstname(nullable:true) //,validator:checkHasValue)
contactLastname(nullable:true) //,validator:checkHasValue)
secondSave(nullable:true,validator:checkHasValue))
}
//use the same validator since it is doing identical check
static checkHasValue={value,obj,errors->
// So if secondSave has a value but contactFirstName
// is null then complain about contactFirstName
// you can see how secondSave gets initialise below
//typical set this to true when you are about to save on 2nd attempt
//then when set run validate() which will hit this block below
// Check all the things you think should have a
// value and reject each field that don't
if (val) {
if ( !obj.contactFirstname) {
errors.rejectValue('contactFirstname',"invalid.contactFirstname")
}
if ( !obj.contactSecondname) {
errors.rejectValue('contactSecondname',"invalid.contactSecondname")
}
//and so on
}
}
现在在控制器中:
class MyController {
def save1(BookingBean bean) {
Booking booking = new Booking()
// this will save all properties
booking.whatEver = bean.whatEver
booking.save()
// you can choose to validate or not here
// since at this point the secondSave has
// not been set therefore validation not called as yet in the bean
}
//您可能有id,它应该与实际的域类绑定
def save2(BookingBean bean) {
booking.secondSave=true
if (!bean.validate()) {
//this is your errors
//bean.errors.allErrors
return
}
//otherwise out of that loop since it hasn't returned
//manually set each object
booking.contactFirstname=bean.contactFirstName
booking.contactSecondname=bean.contactSecondname
booking.save()
}
}
e2a旁注-以上应回答
在创建之前不要验证它。只有在创建了对象并添加了值之后才进行验证。或者,在验证bean中创建一个函数,作为第二次检查的一部分运行。未验证被称为我不理解您问题的具体内容,因此我将提供一些一般性指导,因为我我刚研究过这个问题
def验证其他错误(def字段=null){
def existingErrors=this.errors
def ret=(字段?this.validate(字段):this.validate())
存在错误?.allErrors?.each{error->
this.errors.rejectValue(error.field,error.code)
}
返回(存在错误?.allErrors?错误:ret)
}
我不了解您问题的具体内容,因此我将提供一些一般性指导,因为我刚刚深入了解了这一点