Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populating own error-messages to the grails domain errors

Tags:

grails

I'd like to know, if (and how) I could append some own error-messages to the domain-object after (or before) a validation.

My intention is, I have to check the uploaded file in a form for some attributes (image size etc.) and if something is wrong, I would like to add an error-message which is displayed in the usual grails ".hasErrors" loop.

(And I think I need to have the possibility to express errors in some cross-domain check failure...)

Thanks in advance, Susanne.

like image 735
susi Avatar asked Oct 23 '25 16:10

susi


1 Answers

You can add custom validation errors as described in the errors docs as follows:

class SampleController {

def save() {
  def sampleObject = new SampleObject(params)
  sampleObject.validate()

  if(imageSizeIsTooBig(sampleObject)) {
    sampleObject.errors.rejectValue(
      'uploadedFile',
      'sampleObject.uploadedFile.sizeTooBig'
    )    
}

private def imageSizeIsTooBig(SampleObject sampleObject) {
  // calculation on sampleObject, if size is too big
}

Perhaps, you could even handle your case with a custom validator, so you can call validate() one time and be sure, that all constraints are fulfilled.

like image 96
Mario David Avatar answered Oct 26 '25 22:10

Mario David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!