Results 1 to 4 of 4

Thread: Validation not triggered

  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Unhappy Validation not triggered

    When trying to persist a User object that has a many-to-many relation with a Role object i am getting unexpected behaviour. This at least to my newbie understanding.

    Using a form that submits the User params and trying to persist the user a strange thing occurs. The validation errors on the User object which are there are totally ignored and the User object is persisted.

    Example:
    I have created a User domain class which has a constraint on the firstname [it may not be blank] and email [must be an email adress].

    So i expect when the form params are submited to the controller the .save() or .validate() will complain when i submit a user with NO firstname and NO email.

    But what is see in the database is that the user is persisted without any problems. Also the row in the ROLE_USERS table is nicely filled with the correct id's from the role and user.

    My GSP create page submits the value's to the controller like this:
    Code:
    <g:textField name="firstname" value="${user?.firstname}" size="12"/>
    ..
    and for the role data:
    Code:
    <g:select name="roles" from="${roles}" value="${user?.roles?.id}" optionKey="id" optionValue="name"/>
    I checked the object details using NetBeans debugger and they inspectors tells me that indeed the User object is filled with a blank username, blank email and with a correct role inside it.

    The controller takes the submitted data like this:
    Code:
    def save = {
        def user = new User(params)
        def vresult = user.validate()
        if(user.save(flush:true))
        {
          render "ok"
        }
        else {
          render(view:'create', model: [user:user, roles: Role.list()])
        }
     }
    I did put in the vresult variable to indicate if the validation fails yes or no. When the User has incorrect data is gives false! so that is ok! Still the user.save() method persists the User domain object, despite having validation errors!!!

    I also checked if a incorrect User is saved when there is no many-to-many mapping. So i have a single User object. Then the validation errors are correctly handled. In other words the User is then not persisted as I expect.

    I am having this problem on Grails 1.3.1 and also 1.3.2

    I am wondering what i am missing in this scenario?? Do I need to persist the data in another way? or am I using the many-to-many in a wrong way?

    Code:
    class Role {
    
        String name
        String description
        
        static constraints = {
            name(blank:false, unique:true)
            description(nullable:true)
        }
    
        static hasMany = [users: User]
    }
    
    class User {
    
        String firstname
        String lastname
        String email
        String fogbugzToken
    
        static constraints = {
            firstname(blank:false)
            //email(email:true)
        }
    
        static hasMany = [roles: Role]
        static belongsTo = Role
    }
    
    class UserController { 
    ...
      def save = {
           def user = new User(params)
           def bresult = user.validate()
           if(user.save(flush:true))
           {
               render "ok"
           }
           else {
               render(view:'create', model: [user:user, roles: Role.list()])
           }
       }
    
    ...   
    }
    Last edited by marcopas; Jul 6th, 2010 at 01:03 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

    I'm unable to reproduce this problem. The user instance should not be saved to the database if it has validation errors. In fact, the database should be complaining because the corresponding columns in the USER table should be NOT NULL.

    Please attach a reproducible example to a Grails JIRA issue so we can take a look.

  3. #3
    Join Date
    Jul 2010
    Posts
    4

    Default

    Created Jira issue for this.

    see: http://jira.codehaus.org/browse/GRAILS-6475

  4. #4
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

    Verified the error. Hopefully someone will take a look at it again soon.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •