Announcement Announcement Module
Collapse
No announcement yet.
Update issue of domain classes. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Update issue of domain classes.

    hi all, i'm a newbie about Grails and i have a question about how Grails manage transactions while updating an object domain.

    This is my case:
    Domain class:
    Customer {
    String name
    String code
    static constraints = {
    name(nullable:false,blank:false,unique:true)
    code(nullable:false,blank:false,unique: true)

    }
    }
    In the Customer controller i have this code for update the Customer domain :


    def update = {
    Customer customer = Customer.get(params.id)
    customer.properties = params;
    //here validating the code with my regex or something
    if(!isValidCode(params.code)){
    customer.errors.rejectValue 'code', 'Bad code'
    render view:"edit",model:[customer:customer]
    return;
    }

    if(customer.save()) {
    redirect action:list
    }else {
    render view:"edit",model:[customer:customer]
    }
    }


    Ok the problem is :
    Why if i enter in the block if(!isValidCode(params.code)) , that mean the code have a not allowed value , after the return statement the instance is stored with the wrong value on the DB?
    How can i mark the instance object retrieved via domainClass.get(id) as not qualified to be saved on the DB?
    I use the domainClass.errors.rejectValue('nameProperty','err or message') and i thought that the Framework should recognize as the object is not safe to be saved/updated on the db, but seem that it ignore my errors.
    I have the workaround:
    discard() on the instance and then merge() instead of save() but what i want know is which is the correct way to accomplish an update without the discard() method. how to set an error in a correct manner so Grails will not save my instance on DB.

    Thankin advance for any tip and reply.
    T.

  • #2
    It's best to use the standard GORM validation by adding a custom validator that checks for valid codes. The system for marking domain instances as invalid is fairly complex and can change from version to version, so trying to do it manually isn't a good idea.

    Comment


    • #3
      Thanks for the fast reply.

      Ok so i used this for the code property in the domain class:
      code(nullable:false,blank:false,unique: true
      validator:{ val ->
      Pattern p = Pattern.compile('^[0-9]+[0-9]*$')
      if( val.length() != 11 )
      return false;
      //accept only positive integers
      if(!p.matcher(val).matches()) {
      return false;
      }
      return true
      }
      )



      it is correct to return false or true?
      I seen that it work fine too,when i return an hashMap like this: [message.error.label,value], and if i don't return anything the validate pass and save on db.
      Which way is better return true and false or return just if validation fail?

      Thanks again for the fast reply ^^

      Comment


      • #4
        You have several options, all documented in the reference guide. Use the simplest option that works for you, so if returning 'false' for failed validation works for you, then use that. If you need a custom error message, then return the i18n code as a string.

        Hope that helps.

        Comment

        Working...
        X