Question

I am a grails and groovy newbie. I am trying to update a field in my domain class provided the value for the field is provided by the user in the "Edit" view.

My domain class looks like: class CertificateInfo {

static auditable = [ignore:['dateCreated','lastUpdated','account']]

String entityId
String certificate

String status
Timestamp dateCreated
Timestamp lastUpdated

Account account

CertificateInfo(){
    status = "ACTIVE"
}


static belongsTo = [account: Account]
//Db mappings not included
static constraints = {
    entityId size: 1..300, blank: false, unique: true
    certificate size: 1..4000, blank:false
    status in:['ACTIVE','INACTIVE'], blank: false
}
}

In my edit form, the relevant field is:

<td>
<input type="file" name="certificate" value="${CertificateInfo ?.certificate}"/>
</td>

What I would like to do here is: a) if the new file is provided then on the button click, this file should be accepted as the certificate. This portion works and the new file is used to update the certificate b) if a new file is not provided, then it should just retain the old value. This portion does not work because I do not know how to approach it.

I would be glad for any suggestions.

Thanks!

Was it helpful?

Solution

This is outlined in the user guide - http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles

Using the first example, you would do something like

 def f = request.getFile('certificate')
    if (f.empty) {
        // do nothing
    } else {
        f.transferTo(new File('/some/local/dir/myfile.txt'))
        // modify your object here and save.
        mycertificate.certificate = 'myfile.txt'  
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top