Home I am going to save an User and Organization object in Database but it is not inserting in grails
Reply: 0

I am going to save an User and Organization object in Database but it is not inserting in grails

user6337
1#
user6337 Published in April 19, 2018, 10:01 am

i want to save the User and the organization from this createorg.gsp and i enterted the textfield and it is not showing me any error and is not inserting into the database.User and Organization are two different domain here and the action is moving to def saveOrg().

createorg.gsp

<g:uploadForm method="POST" action="save">
        <h1>Create Organization</h1>
        <h3>Enter Organization Name</h3>
        <g:textField name="orgName" placeholder="Enter organiation name"/><br>
        <h3>Enter Organization Establishment</h3>
        <g:textField name="orgEsta" placeholder="Enter organiation Establishment Date"/><br>
        <!--for uploading image-->
        <h3>Choose Organization Image</h3>
        <input type="file" name="getFile" placeholder="Choose Organization image"/>

        <h3>Choose Organization Description</h3>
        <g:textField name="orgDesc" placeholder="Enter organiation description"/><br>
        <h3>Choose Organization location</h3>
        <g:textField name="orgLoc" placeholder="Enter organiation location"/><br>
        <!--g:submitButton name="save" value="save"/-->

                    <h3>Enter Username</h3>
                    <g:textField name="username" placeholder="Enter UserName"/><br>
                    <h3>Enter Password</h3>
                    <g:textField name="password" id="pass1" placeholder="Enter Password" /><br>
                     <h3>Confirm Password</h3>
                     <g:textField name="password" id="pass2" placeholder="Confirm Password" onkeyup='check();'/><br>
                 <div class="alert alert-success" style="display: none" id="showAlert">
                    <strong><span id='message'></span></strong>
                </div>
                <div class="alert alert-success" style="display: none" id="showAlert1">
                    <strong><span id='message1'></span></strong>
                </div>

                 <g:submitButton  class="btn btn-primary" name="saveOrg" value="save" id="submit" />

    </g:uploadForm>
        </div>
        <script>
            var check = function() {
                if (document.getElementById('pass1').value ==
                    document.getElementById('pass2').value) {
                    document.getElementById('showAlert').style.display = 'block';
                    document.getElementById('message').style.color = 'green';
                    document.getElementById('message').innerHTML = 'Password Finally Matched';
                    document.getElementById('submit').disabled = false;
                    if(document.getElementById('pass1').value.length<5 && document.getElementById('pass2').value.length<5){
                        document.getElementById('showAlert').style.display = 'block';
                        document.getElementById('message').style.color = 'red';
                        document.getElementById('message').innerHTML = 'Pasword Minimum Length must be 5character';
                        document.getElementById('submit').disabled = false;
                    }
                } else {
                    document.getElementById('message').style.color = 'red';
                    document.getElementById('message').innerHTML = 'Passpord Not Matching';
                    document.getElementById('submit').disabled = true;
                }
            }

</script>
    </body>
</html>

organization.gsp

package com.ashwin

class Organization {


    String orgName
    String orgEsta
    String orgLogo
    String orgDesc
    String orgLoc

    static belongsTo = [user:User]


    static constraints = {
    }

   }

User.gsp

package com.ashwin

import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import grails.compiler.GrailsCompileStatic

@GrailsCompileStatic
@EqualsAndHashCode(includes='username')
@ToString(includes='username', includeNames=true, includePackage=false)
class User implements Serializable {

    private static final long serialVersionUID = 1

    String username
    String password
    String cv=null


    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    Set<Role> getAuthorities() {
        (UserRole.findAllByUser(this) as List<UserRole>)*.role as Set<Role>
    }

    static constraints = {
        password nullable: false, blank: false, password: true
        username nullable: false, blank: false, unique: true
    }

    static mapping = {
        password column: '`password`'
    }
}

UserController.gsp

@Secured('permitAll')
    def saveOrg() {
        def userName = params.username
        println(userName)
        try {

            userService.save(user)
            def getFile = params.getFile
            println(params)
//        println "-------------------"+getFile.getOriginalFilename()
            def okContentTypes = ['image/jpeg','image/png','image/jpg']//application/pdf

            if(!okContentTypes.contains(getFile.getContentType())){
                render "invalid image"
            }else {
                def fileUploaded = organizationService.uploadImage(getFile, getFile.getOriginalFilename(),
                        "assets/images")
                println "fileUploaded = $fileUploaded"
                if (fileUploaded) {
                    println "here"
                    def org =new Organization(orgName:params.orgName,orgEsta:params.orgEsta,orgLogo: getFile.getOriginalFilename(),orgDesc:params.orgDesc,orgLoc:params.orgLoc,user:user)
                    org.save(flush: true)
                    new UserRole(user:user,role:Role.findByAuthority('ROLE_ORG')).save(flush: true, failOnError: true)
                    redirect (view: "/organization/index")
                }
            }


        } catch (ValidationException e) {
            println(e.message)
        }

           }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.29867 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO