Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails rejectValue-导致ob.errors null的多个检查_Grails - Fatal编程技术网

Grails rejectValue-导致ob.errors null的多个检查

Grails rejectValue-导致ob.errors null的多个检查,grails,Grails,“我的域对象预订”有多个允许为空的属性,因为这些属性将在对象保存到数据库之后设置 myService.action()的一部分: 我正在使用 hasErrors(bean:booking,字段:'contactFirstname','has error')) 标记错误字段 如果我现在提交的表单中文本字段中没有任何值,则所有字段都是红色,booking.errors的错误大于0 如果我在提交表单后使用名字,booking.errors为NULL,并且没有标记其他字段 这是虫子吗?我支持Grails

“我的域对象预订”有多个允许为空的属性,因为这些属性将在对象保存到数据库之后设置

myService.action()的一部分:

我正在使用 hasErrors(bean:booking,字段:'contactFirstname','has error'))

标记错误字段

如果我现在提交的表单中文本字段中没有任何值,则所有字段都是红色,booking.errors的错误大于0

如果我在提交表单后使用名字,booking.errors为NULL,并且没有标记其他字段

这是虫子吗?我支持Grails2.3.6

其他信息

  • 我访问了表格,完全空着提交
  • 我看到所有表单字段都是红色的,object.errors有>0个错误(有效)
  • 我在第一个字段firstname和submit中输入一个值
  • 我没有看到任何红色的表单字段,object.errors=0错误(无效)
  • 我重新提交表格,没有任何更改
  • 我看到所有的空表单字段都是红色的,object.errors有>0个错误(有效)

  • 现在我已经完全了解了情况,而且因为我睡眠困难,我想我会给你一个非常简洁的答案,这样你就有希望完全理解并正确地使用东西

    首先,我知道创建一个验证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中创建一个函数,作为第二次检查的一部分运行。未验证被称为

    我不理解您问题的具体内容,因此我将提供一些一般性指导,因为我我刚研究过这个问题

  • 在validate()之前不要调用hasErrors()。如果这样做,Grails不会将域约束中的错误传递给您,您只会得到使用rejectValue()自己设置的错误

  • 使用rejectValue()时要小心。请尝试使用域约束设置所有错误。如果您有复杂的约束,请使用验证程序语法和obj。getPersistentValue()可能偶尔是您的朋友

  • 如果您仍然必须使用rejectValue(),请理解以后对validate()的任何调用都将从头开始并删除您以前的错误。我已经为此编写了一个解决方法(放置在您的域对象中),尽管我不能向您保证这是100%确定的:

  • 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)
    
    }

    我不了解您问题的具体内容,因此我将提供一些一般性指导,因为我刚刚深入了解了这一点

  • 在validate()之前不要调用hasErrors()。如果这样做,Grails不会将域约束中的错误传递给您,您只会得到使用rejectValue()自己设置的错误

  • 小心